FFmpeg How to write video to a file

2019-02-01 12:26发布

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).

Best Wishes

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

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

Some Extra Info

In FFmpeg I have to

  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)

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.

标签: c ffmpeg
4条回答
叼着烟拽天下
2楼-- · 2019-02-01 12:32

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

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

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

查看更多
三岁会撩人
3楼-- · 2019-02-01 12:34

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

This is for the current ffmpeg 2.4.4

查看更多
仙女界的扛把子
4楼-- · 2019-02-01 12:48

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

ffmpeg encoding sample wanted?

Encode audio to aac with libavcodec

查看更多
一夜七次
5楼-- · 2019-02-01 12:52

Probably this link may help you:

Output a media file in any supported libavformat format

I hope it helps.

查看更多
登录 后发表回答