stream byte arrays to a video using ffmpeg

07
2014-07
  • Andrew Simpson

    This is what I am trying to do:

    I receive jpegs from an IP camera. If motion is detected I want to upload the jpeg still shot to my server.

    To save on bandwidth I save each jpeg to a file on my hard drive and I give it a sequence number.

    I then use ffmpeg to create a AVI file.

    I then load this AVI to a memory stream and upload the byte array of that stream to my server using WCF.

    As each jpeg is saved to the hard drive there is an overhead of doing so. If it was possible to skip this step and stream these jpegs directly as an input to an avi file using ffmpeg then I will save time and memory.

    This may be easy (I do not know if it is) on non-windows platforms and/or using C++ but I am using C# on Windows 7.

    I can add the code I use to convert images to an avi file if requested and people think it is pertinent?

  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    FFmpeg convert video w/ dropped frames, out of sync
  • preahkumpii

    I recorded a video using Bandicam with the MJPEG encoder to get the least amount of lag. Now, I am trying to convert that massive file to a h264 avi using ffmpeg. I know there are dropped frames in the video stream...more than 100 in the first two minutes, which I assume is simply because Bandicam dropped some when it couldn't keep up. So, when I convert the file to h264, the video and audio are out of sync, and appear to be more and more out of sync as output video progresses. Here is my basic command in ffmpeg:

    ffmpeg -i "C:\...\input.avi" -vcodec libx264 -q 5 -acodec libmp3lame -ar 44100 -ac 2 -b:a 128k "C:\...\output.avi"
    

    I have tried EVERYTHING I can think of including:

    -itsoffset [-]00:00:01
    

    Tried this before and after input file. This doesn't work because as the video progresses it becomes more and more out of sync.

    -async 1
    

    Doesn't work.

    -vsync 1
    

    Doesn't work, but it does show dropped frames being duplicated.

    Two inputs of same file with mapping using -map 0:0 -map 1:1. Doesn't work.

    The source plays just fine. Any ideas how to convert it with ffmpeg and keep the audio and video synced? Thanks.


  • Related Answers
  • Justin Buser

    It may have dropped frames if they were duplicates, if the source plays fine then your problem is more likely related to the fact that AVI containers don't handle VBR encoded formats very well (h264 in particular doesn't play well with AVI). I'd recommend starting by dumping the video to an uncompressed AVI for testing as it's the most consistent format to encode from

    ffmpeg -i input.avi -acodec copy -vcodec rawvideo output.avi
    

    Then, assuming that plays in sync you should try doing a straight codec copy and remux the source into a different format, i.e.:

    ffmpeg -i input.avi -acodec copy -vcodec copy output.mkv
    

    MKV will usually handle just about anything you stuff in it without a problem. I even hide some encrypted zip files in various mkv videos on my computer since you can encode file attachments in MKVs. So if either of that works without sync issues then try the same thing but re-encode the video or audio, but only one or the other to start and keep the container format consisten so you know where the issue lies. i.e.:

    ffmpeg -i input.avi -acodec copy -vcodec libx264 output.mkv
    

    or

    ffmpeg -i input.avi -acodec libmp3lame -vcodec copy output.mkv
    

    Try to make each test conversion as simple as possible, i.e. don't change bitrate/sample rate, use the example parameters I listed first. After each one you'll be one step closer to knowing exactly what you can and can't do. If you have to have h.264 I'd recommend going with an mp4 container as it's far more compatible than avi, on the other hand if the requirement is an avi container then I'd recommend wmv3 or msvideo1 for video as they would be the most likely to encode properly.

  • Bon Gart

    Almost always, when audio and video are out of sync and difference between the two get progressively worse over the course of the movie, especially after a conversion, the issue is one where the audio and video components were of a different length. As was observed in this instance, this happened because video frames were getting dropped, but the audio was relatively untouched during the conversion.

    At times like this, it is best to first check the source movie to ensure that the audio was indeed in sync. If it is, then demux the movie, and deal with the video and audio components separately.