0

I have been working on a video processing tool that takes each frame from an input video and saves the result as a series of images. So, in essence, if my video has a length of 5 minutes, and has 25 fps, I will get 7500 individual frames.

What I want is then to put this back into a video format, but at the same time also add the audio track from the original source. Is this possible with ffmpeg? Also, would it be possible to automatically determine the fps on the original video source and then use this as fps value for ffmpeg for the video + audio track in the new video file?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

2 Answers2

0

Step 1: Create PNGs based on source file: source.mp4

ffmpeg -i source.mp4 img_%06d.png

The command creates PNG files based on the input file. %06 formats the number with 6 digits (e.g. %06d => 000099 instead of 99).

Step 2: Build a text file with all images (used as input in step 3).

for f in ./img_*.png; do echo "file '$f'" >> mylist.txt; done

For more details check out the concatenate page on FFmpeg wiki

Step 3: Encode video (with images) and add audio:

ffmpeg -f concat -r 25 -safe 0 -i mylist.txt -i source.mp4 -c:v libx264 -map 0:v -map 1:a output.mp4
  • -f concat merges the next input file content
  • -r 25 is 25fps
  • -safe 0 allow relative paths in text file
  • -i source.mp4 adds the original file (used for the audio)
  • -c:v libx264 creates a new h264 video
  • -map 0:v -map 1:a use the video of the first input (our concatenated video) and the audio of the second input file (the audio of the original video)

You can also detect the FPS of the source video: get video fps using FFProbe

martinr92
  • 588
  • 1
  • 7
  • 15
0

Frames to video with audio

ffmpeg -framerate 25 -i %04d.png -i original.mp4 -map 0 -map 1:a -c:a copy output.mp4
  • Add -vf format=yuv420p output option for player compatibility if you're outputting H.264 or HEVC.
  • Assuming your inputs are named 0001.png, 0002.png, 0003.png, etc. See the image demuxer documentation for more info.
  • This assumes your original source has constant frame rate (CFR) and not variable frame rate (VFR).

Determine frame rate of original video source

ffprobe -v error -select_streams v -show_entries stream=r_frame_rate -of csv=p=0 input.mp4

Example output:

30000/1001

Which means use -framerate 30000/1001.

This also assumes your original source is CFR.

llogan
  • 121,796
  • 28
  • 232
  • 243