title: Combine Audio and Images in Stream
body: <p>I would like to be able to create images on the fly and also create audio on the fly too and be able to combine them together into an rtmp stream (for Twitch or YouTube). The goal is to accomplish this in Python 3 as that is the language my bot is written in. Bonus points for not having to save to disk.</p>
  
  <p>So far, I have figured out how to stream to rtmp servers using ffmpeg by loading a PNG image and playing it on loop as well as loading a mp3 and then combining them together in the stream. The problem is I have to load at least one of them from file.</p>
  
  <p>I know I can use Moviepy to create videos, but I cannot figure out whether or not I can stream the video from Moviepy to ffmpeg or directly to rtmp. I think that I have to generate a lot of really short clips and send them, but I want to know if there's an existing solution.</p>
  
  <p>There's also OpenCV which I hear can stream to rtmp, but cannot handle audio.</p>
  
  <p>A redacted version of an ffmpeg command I have successfully tested with is</p>
  
  <pre><code>ffmpeg -loop 1 -framerate 15 -i ScreenRover.png -i "Song-Stereo.mp3" -c:v libx264 -preset fast -pix_fmt yuv420p -threads 0 -f flv rtmp://SITE-SUCH-AS-TWITCH/.../STREAM-KEY
  </code></pre>
  
  <p>or</p>
  
  <pre><code>cat Song-Stereo.mp3 | ffmpeg -loop 1 -framerate 15 -i ScreenRover.png -i - -c:v libx264 -preset fast -pix_fmt yuv420p -threads 0 -f flv rtmp://SITE-SUCH-AS-TWITCH/.../STREAM-KEY
  </code></pre>
  
  <p>I know these commands are not set up properly for smooth streaming, the result manages to screw up both Twitch's and Youtube's player and I will have to figure out how to fix that.</p>
  
  <p>The problem with this is I don't think I can stream both the image and the audio at once when creating them on the spot. I have to load one of them from the hard drive. This becomes a problem when trying to react to a command or user chat or anything else that requires live reactions. I also do not want to destroy my hard drive by constantly saving to it.</p>
  
  <p>As for the python code, what I have tried so far in order to create a video is the following code. This still saves to the HD and is not responsive in realtime, so this is not very useful to me. The video itself is okay, with the one exception that as time passes on, the clock the qr code says versus the video's clock start to spread apart farther and farther as the video gets closer to the end. I can work around that limitation if it shows up while live streaming.</p>
  
  <pre><code>def make_frame(t):
    img = qrcode.make("Hello! The second is %s!" % t)
    return numpy.array(img.convert("RGB"))
  
  clip = mpy.VideoClip(make_frame, duration=120)
  clip.write_gif("test.gif",fps=15)
  
  gifclip = mpy.VideoFileClip("test.gif")
  gifclip.set_duration(120).write_videofile("test.mp4",fps=15)
  </code></pre>
  
  <p>My goal is to be able to produce something along the psuedo-code of</p>
  
  <pre><code>original_video = qrcode_generator("I don't know, a clock, pyotp, today's news sources, just anything that can be generated on the fly!")
  original_video.overlay_text(0,0,"This is some sample text, the left two are coordinates, the right three are font, size, and color", Times_New_Roman, 12, Blue)
  original_video.add_audio(sine_wave_generator(0,180,2)) # frequency min-max, seconds
  
  # NOTICE - I did not add any time measurements to the actual video itself. The whole point is this is a live stream and not a video clip, so the time frame would be now. The 2 seconds list above is for our psuedo sine wave generator to know how long the audio clip should be, not for the actual streaming library.
  
  stream.send_to_rtmp_server(original_video) # Doesn't matter if ffmpeg or some native library
  </code></pre>
  
  <p>The above example is what I am looking for in terms of video creation in Python and then streaming. I am not trying to create a clip and then stream it later, I am trying to have the program be able to respond to outside events and then update it's stream to do whatever it wants. It is sort of like a chat bot, but with video instead of text.</p>
  
  <pre><code>def track_movement(...):
    ...
    return ...
  
  original_video = user_submitted_clip(chat.lastVideoMessage)
  original_video.overlay_text(0,0,"The robot watches the user's movements and puts a blue square around it.", Times_New_Roman, 12, Blue)
  original_video.add_audio(sine_wave_generator(0,180,2)) # frequency min-max, seconds
  
  # It would be awesome if I could also figure out how to perform advance actions such as tracking movements or pulling a face out of a clip and then applying effects to it on the fly. I know OpenCV can track movements and I hear that it can work with streams, but I cannot figure out how that works. Any help would be appreciated! Thanks!
  </code></pre>
  
  <p>Because I forgot to add the imports, here are some useful imports I have in my file!</p>
  
  <pre><code>import pyotp
  import qrcode
  from io import BytesIO
  from moviepy import editor as mpy
  </code></pre>
  
  <p>The library, pyotp, is for generating one time pad authenticator codes, qrcode is for the qr codes, BytesIO is used for virtual files, and moviepy is what I used to generate the GIF and MP4. I believe BytesIO might be useful for piping data to the streaming service, but how that happens, depends entirely on how data is sent to the service, whether it be ffmpeg over command line (from subprocess import Popen, PIPE) or it be a native library.</p>