FFmpeg如何将video写入文件

我想要的是

1. Get video packet from stream source 2. Decode it 3. And write that decoded data as video file(avi, mpeg etc) 

我可以从文件中获取video数据包(如AVPacket),也可以解码并保存为图像。(原始)(FFmpeg教程显示如何操作)。 但我不能 (不知道) 将video数据写入可由媒体播放器(如VLC)播放的文件 (其他)。

最好的祝愿

Ps:如果可能的话,真正的代码样本会很棒……

现在我用av_interleaved_write进行测试但是我得到了奇怪的错误“非单调时间戳”(我无法控制媒体源的pts值)

一些额外的信息

在FFmpeg,我必须

  1. 从媒体源读取媒体数据包(可能是真实文件(.avi,mov)甚至是rtsp服务器)。
  2. 然后将这些媒体数据包写入真实文件(物理.avi,.mov等文件)

我需要读者和作家。 我可以读取媒体源文件(甚至根据给定的格式对数据包进行编码)。 但我不能写文件…(任何玩家都可以玩)

还有一些伪代码

 File myFile("MyTestFile.avi"); while ( source ->hasVideoPackets) { packet = source->GetNextVideoPacket(); Frame decodedFrame = Decode(packet); VideoPacket encodedPacket = Encode( decodedFrame); myFile.WriteFile(encodedPacket); } 

或者只是编写没有编码解码的原始文件

  File myFile("MyTestFile.avi"); while ( source ->hasVideoPackets) { packet = source->GetNextVideoPacket(); myFile.WriteFile(packet); } 

然后

 I can able to open MyTest.avi file with a player. 

这个链接可能对您有所帮助:

以任何支持的libavformat格式输出媒体文件

我希望它有所帮助。

现在在FFmpeg trunk的doc / examples / transcoding.c中有一个新的video转码示例 ,它完全符合您的需求: 解复用,解码,过滤,编码和复用的API示例。

这是针对当前的ffmpeg 2.4.4

我在某些时候使用libx264和vorbis做了类似的事情。

一个代码示例。 https://github.com/Themaister/SSNES/blob/master/record/ffemu.c

基本思想是,当您想对其进行编码时,必须自己在AVFrame设置时间戳。 然后你可以获取该数据包并使用av_interleaved_write()编写它。

您需要编写一个编码器来将原始数据转换为所需的video格式

想要ffmpeg编码样本吗?

使用libavcodec将音频编码到aac