Ramalingam Perumal
Ramalingam Perumal

Reputation: 1427

Convert video from image and audio files using FFMPEG

Here i am converted video from images(8) and audio(file size: 307KB). But the video is playing one image with audio and others are without audio. In my knoledge : Because the audio file size is very low. I want to converted video playing all images with audio. Sample code :

$audioPath = 'upload/2016/01/tmp_81332/audio_1453307709.wav';
$convAudioPath = 'upload/2016/01/tmp_81332/output.mp3';
$ffmpeg = '/usr/bin/ffmpeg';

$convImgPath = 'upload/2016/01/tmp_81332/image%d.jpeg';
$output = 'upload/2016/01/video_81332_1453307709.mp4';

exec("$ffmpeg -i $audioPath -f mp2 $convAudioPath");
exec("$ffmpeg -framerate 1/5 -i $convImgPath -i $convAudioPath -c:v libx264 -c:a aac -strict experimental -b:a 16k -shortest $output");

However this extends the output video file to be the length of the audio file if it is longer than the video. Using -shortest cuts the video file short if the audio file is shorter than the video. So is there a flag to tell ffmpeg to cut the keep the length of the output video to the length of the input video?

Upvotes: 2

Views: 2357

Answers (2)

Gyan
Gyan

Reputation: 92928

Use

exec("$ffmpeg -framerate 1/5 -i $convImgPath -i $audioPath -af apad -c:v libx264 -r 5 -c:a aac -strict experimental -b:a 16k -shortest $output");

This pads the audio with a null audio source of potentially infinite length, so the video will always be the shortest input.

Upvotes: 1

Balaraman L
Balaraman L

Reputation: 196

Audio conversion from wav format to mp3 is NOT needed in your case. We can do it in single command. Problem of displaying only 1 image can be solved by setting output video frame rate by -r option.

Replace your 2 exec statements with the following one and it should work.

exec("$ffmpeg -framerate 1/5 -i $convImgPath -i $audioPath -c:v libx264 -r 30 -c:a aac -strict experimental -b:a 16k $output");

Reference: https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images

I have also removed the -shortest flag in previous command. So, output would be either the length of the video (if that was greater than length of input audio) or it would be length of audio otherwise.

So, you can cut the required video part starting from 0th second to nth second where video ends. That n can be found out as you already know the input frame rate (each picture gets displayed for 5 seconds because of -framerate 1/5 flag) and the number of images (8).

So, n = 8 x 5 = 40 seconds.

So, always cut video from 0 to 40th second after the first ffmpeg command. This can be achieved with following ffmpeg command.

ffmpeg -i INFILE.mp4 -vcodec copy -acodec copy -ss 00:00:00.000 -t 00:00:40.000 OUTFILE.mp4

Hope this helps.

Upvotes: 3

Related Questions