C语言 FFmpeg 如何将视频写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5107900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:52:22  来源:igfitidea点击:

FFmpeg How to write video to a file

cffmpeg

提问by NoviceAndNovice

What i want is

我想要的是

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

I can able to get video Packets from a file (as AVPacket) and also can decode and save as an image.(raw)( FFmpeg tutorials show how to do it). But i can not( do not know ) write that video data to a file(other) which can be played by media players(such as VLC).

我可以从文件中获取视频数据包(作为 AVPacket),也可以解码并保存为图像。(原始)(FFmpeg 教程展示了如何做到这一点)。 但是我不能(不知道)将该视频数据写入可以由媒体播放器(例如 VLC)播放的文件(其他)。

Best Wishes

最好的祝愿

Ps: Real code samples will be great if possible...

Ps:如果可能的话,真实的代码示例会很棒......

Now i make test with av_interleaved_writebut i got strange error "non monotone timestamps" ( i have no control over pts values of media source )

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

Some Extra Info

一些额外的信息

In FFmpeg I have to

在 FFmpeg 我必须

  1. Read media packets from media source ( it may be real file(.avi,mov) or even rtsp server).
  2. Then write those media packets to a real file (physical .avi, .mov etc file)
  1. 从媒体源(可能是真实文件(.avi、mov)甚至rtsp服务器)读取媒体包。
  2. 然后将这些媒体数据包写入真实文件(物理 .avi、.mov 等文件)

I need reader and writer. I can read the media source file ( even encode packets according to given format). But i can not write to file...(which any player can play)

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

And some pseudoCode

还有一些伪代码

File myFile("MyTestFile.avi");

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

Or Just write the original file without encode decode

或者只写原始文件而不编码解码

     File myFile("MyTestFile.avi");

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

Then

然后

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

回答by Mixaz

Now there's a new example of video transcoding in doc/examples/transcoding.cof FFmpeg trunk, and it does exactly what you need: API example for demuxing, decoding, filtering, encoding and muxing.

现在在FFmpeg 主干的doc/examples/transcoding.c中有一个新的视频转码示例,它完全满足您的需求:用于解复用、解码、过滤、编码和复用的 API 示例。

This is for the current ffmpeg 2.4.4

这是针对当前的ffmpeg 2.4.4

回答by Aditya P

you need to write an encoder to convert raw data to required video format

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

ffmpeg encoding sample wanted?

Encode audio to aac with libavcodec

想要 ffmpeg 编码示例?

使用 libavcodec 将音频编码为 aac

回答by Maister

I did something like this at some point using libx264 and vorbis.

我曾使用 libx264 和 vorbis 做过类似的事情。

A code example. https://github.com/Themaister/SSNES/blob/master/record/ffemu.c

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

The basic idea is that you have to set timestamps yourself in the AVFramewhen you want to encode it. Then you can take that packet and write it with av_interleaved_write().

基本思想是您必须在AVFrame要对其进行编码时自己设置时间戳。然后你可以拿那个数据包并用av_interleaved_write().