简体   繁体   中英

Write H.264 stream in buffer to a streamable mp4 using ffmpeg

I wrote code to create H.264 stream, which has a loop to generate H.264 encoded frame.

while(true) {
  ...
  x264_encoder_encode(encoder, &buffer, &i_buffer, &pic_in, &pic_out);
  ...
  /*TODO: Write one frame in the buffer to a streamable mp4 file*/
}

MP4 is actually not streamable. So in other words, you can't do it at all. I ran in that very problem.

The reason why it won't work is because when you open an mp4 file, you have to have all sorts of parameters, which by default get saved at the end of the file. When you create an MP4, you can always forcibly save that info at the start. However, to know what those parameters are, you need all the data. And without those parameters, the software trying to load the mp4 fails very early on. This is true for some other formats such as webm videos and .m4a or .wav for audio.

What you have to do is stream the actual H.264, possibly using RTSP or a format of your own if you're in control of both sides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM