Sir DrinksCoffeeALot
Sir DrinksCoffeeALot

Reputation: 633

FFmpeg decoding .mp4 video file

I'm working on a project that needs to open .mp4 file format, read its frames 1 by 1, decode them and encode them with better type of lossless compression, and save them into a file.

From my understanding, it should go like this:

  1. Open input .mp4 file
  2. Find stream info -> find video stream index
  3. Copy codec pointer of found video stream index into AVCodecContext type pointer
  4. Find decoder -> allocate codec context -> open codec
  5. Read frame by frame -> decode the frame -> encode the frame -> save it into a file

So far, I encountered a couple of problems. For example, if I want to save a frame using av_interleaved_write_frame() function. I can't open input a .mp4 file using avformat_open_input() since it's going to populate filename part of the AVFormatContext structure with the input file name, and therefore, I can't "write" into that file.

I've tried a different solution using av_guess_format(), but when I dump format using dump_format(), I get nothing, So I can't find stream information about which codec it is using.

How should this be done?

Upvotes: 4

Views: 15496

Answers (2)

Ronald S. Bultje
Ronald S. Bultje

Reputation: 11184

See the "detailed description" in the muxing docs. You:

  1. set ctx->oformat using av_guess_format
  2. set ctx->pb using avio_open2
  3. call avformat_new_stream for each stream in the output file. If you're re-encoding, this is by adding each stream of the input file into the output file.
  4. call avformat_write_header
  5. call av_interleaved_write_frame in a loop
  6. call av_write_trailer
  7. close the file (avio_close) and clear up all allocated memory

Upvotes: 6

Eugene
Eugene

Reputation: 11545

You can convert a video to a sequence of losses images with:

ffmpeg -i video.mp4 image-%05d.png

and then from a series of images back to a video with:

ffmpeg -i image-%05d.png video.mp4

The functionality is also available via wrappers.

You can see a similar question at: Extracting frames from MP4/FLV?

Upvotes: -3

Related Questions