user2650277
user2650277

Reputation: 6729

Combining an image sequence with audio and make a video

I am trying to take a video extract the frames and the audio.Then i do some compression on the frames and then i want to combine the image frames with the audio to create the video.

1.Create frames from video based on fps (lossless)

ffmpeg  -i  big_buck_bunny_480p_surround-fix.avi -q:v 1 ./vidtest/out%d.jpg

The problem here is that the quality of the image is 94 based on graphicsmagick , how can i extract the frames at original quality.

2.Getting the audio

ffmpeg -i big_buck_bunny_480p_surround-fix.avi  -vn -acodec copy  big_buck_bunny_480p_surround-fix.ac3

3.How do i combine the audio with the image sequences

Upvotes: 0

Views: 1924

Answers (1)

AkselA
AkselA

Reputation: 8837

It seems like you already understand how to extract the audio stream from a video, so we'll skip that step.

Let's assume you have a video file named video.mp4 and an audio file named audio.mp3. You want to take stills from the video and create a slideshow with audio.mp3 as the audio track.

First we'll extract the stills. fps=1/10 means that frames will be extracted every tenth second. -ss 00:00:5 means that we'll begin five seconds from the start. The first bit of a video is often a fade-in containing mostly black, so it might be desirable to skip that.

ffmpeg -ss 00:00:5 -i video.mp4 -vf fps=1/10 image%03d.png

Next we'll count how many images that resulted in. This will be handy to know when we're specifying the frame rate of the slideshow.

imgcount=$(find . -maxdepth 1 -name "image*" | wc -l)

echo $imgcount

The duration of the audio track would also be nice to know. With MP3 and similar formats, especially when encoded with a variable bit rate, estimation of duration can be fraught. To get around this the audio file can be converted into a WAV file, f.ex, and then the duration estimated.

adur=$(ffprobe -v error -select_streams a:0 -show_entries stream=duration \
  -of default=noprint_wrappers=1:nokey=1 audio.mp3)

echo $adur

Now we'll recombine the images into a slideshow. $imgcount/$adur specifies the display rate of the stills so the duration matches that of the audio file reasonably well. -r 25 specifies the actual frame-rate of the video. Not all formats and video players accept unusual frame-rates.

ffmpeg -framerate $imgcount/$adur -i image%03d.png -c:v libx264 -r 25 \
  -pix_fmt yuv422p slideshow.mp4

This will mux the audio file and the slideshow file and trim to the duration of the shortest of the two. This works if the container format supports the codec of the audio file, if not either the audio file has to be re-converted, or another container format chosen. I find Matroska (.mkv) very useful. In the case of .avi and .ac3 or .mp4 and .mp3 there should be no problem.

ffmpeg -i slideshow.mp4 -i audio.mp3 -codec copy -shortest slideshow-sound.mp4

Upvotes: 1

Related Questions