My task
The program dynamically generates videos in chunks of several minutes and puts everything in a folder (generated/0.mp4
, generated/1.mp4
, etc.). I need to stream them on YouTube consecutively, without any gaps between files.
The question itself
How can I alternately and continuously write frames to the stream, replacing the original video files on the fly – i.e., after sending the 0.mp4
, start sending 1.mp4
without any delay?
My attempts
Initially, I found a solution using the vidgears
module:
# Minimal example
from vidgear.gears import CamGear, WriteGear
output_params = {
"-acodec": "aac",
"-ar": 44100,
"-b:a": 712000,
"-vcodec": "libx264",
"-preset": "medium",
"-b:v": "4500k",
"-bufsize": "512k",
"-pix_fmt": "yuv420p",
"-f": "flv"
}
current_chunk_num = 0
writer = WriteGear(
output = f"rtmp://a.rtmp.youtube.com/live2/{MY_YT_TOKEN}",
logging = True,
**output_params
)
stream = CamGear(
source = "generated/0.mp4",
logging = True
).start()
try:
while True:
frame = stream.read()
if frame is None:
current_chunk_num += 1
stream.stop()
stream = CamGear(
source = f"generated/{current_chunk_num}.mp4",
logging = True
).start()
writer.write(frame)
except KeyboardInterrupt:
stream.stop()
writer.close()
…however, when trying to redefine stream
, incomprehensible things happen like
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001c278113040] moov atom not found
with the further script termination.
- I found the
PyLiveStream
module, but as far as I understood from the page on PyPI, it is a console utility unsuitable for use from my own program.
Can I (how?) adapt vidgears
to this kind of task, or should other libraries be used to implement it?