2

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*/
}

Every single time, an H.264 encoded frame is generated and stored in the buffer. How can I write it into a streamable mp4 file directly through the buffer?

I spent lots of time searching for the solution. All I can find is to read stream from a file using

avformat_open_input(&fmtCtx, in_filename, 0, 0)

Is there any way to read directly from buffer without a file?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Galaxy
  • 853
  • 2
  • 11
  • 28
  • If I'm reading this correct, you don't need `avformat_open_input`, but an output. – halfelf Nov 21 '16 at 08:19
  • @halfelf I read some code. They use `avformat_open_input` to read an input stream from file, and then write out the output stream after processing. – Galaxy Nov 21 '16 at 08:37
  • What you need is encoding those frames and writing to a file, right? If so, you need output. – halfelf Nov 21 '16 at 09:13
  • Yes. I need output each frame with mp4 or flv as container and stream it. I'm looking into ffmpeg and gstreamer. I don't know which one is more suitable for this task and easier to implement. – Galaxy Nov 21 '16 at 09:19
  • FFmpeg has a `muxing.c` example, that shows how to encode frames and write them to a container. – halfelf Nov 21 '16 at 10:15

1 Answers1

0

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.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156