简体   繁体   English

使用 ffmpeg 将缓冲区中的 H.264 流写入可流式传输的 mp4

[英]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.我编写了代码来创建 H.264 流,它有一个循环来生成 H.264 编码帧。

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. MP4 实际上是不可流式传输的。 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.它不起作用的原因是,当您打开一个 mp4 文件时,您必须拥有各种参数,默认情况下这些参数会保存在文件末尾。 When you create an MP4, you can always forcibly save that info at the start.创建 MP4 时,您始终可以在开始时强制保存该信息。 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.如果没有这些参数,尝试加载 mp4 的软件很早就会失败。 This is true for some other formats such as webm videos and .m4a or .wav for audio.这适用于其他一些格式,例如 webm 视频和 .m4a 或 .wav 音频。

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.您需要做的是流式传输实际的 H.264,如果您可以控制双方,可能使用RTSP或您自己的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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