Combine one image + one audio file to make one video using FFmpeg
The order of options in the command line matters. The following works for my case:
ffmpeg -loop 1 -i img.jpg -i music.mp3 -shortest -acodec copy -vcodec mjpeg result.mkv
In a more general case, where image.jpg
and audio.wav
are your input, you can use the following command, adapted from the FFmpeg wiki:
ffmpeg -loop 1 -i ima.jpg -i audio.wav -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest out.mp4
This would use the libx264
encoder and provide you with better compression than the MJPEG codec used above. The audio is AAC, with the built-in ffmpeg
AAC encoder.
How to create a video from images with FFmpeg?
See the Create a video slideshow from images – FFmpeg
If your video does not show the frames correctly If you encounter problems, such as the first image is skipped or only shows for one frame, then use the fps video filter instead of -r for the output framerate
ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4
Alternatively the format video filter can be added to the filter chain to replace -pix_fmt yuv420p like "fps=25,format=yuv420p". The advantage of this method is that you can control which filter goes first
ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4
I tested below parameters, it worked for me
"e:\ffmpeg\ffmpeg.exe" -r 1/5 -start_number 0 -i "E:\images\01\padlock%3d.png" -c:v libx264 -vf "fps=25,format=yuv420p" e:\out.mp4
below parameters also worked but it always skips the first image
"e:\ffmpeg\ffmpeg.exe" -r 1/5 -start_number 0 -i "E:\images\01\padlock%3d.png" -c:v libx264 -r 30 -pix_fmt yuv420p e:\out.mp4
making a video from images placed in different folders
First, add image paths to imagepaths.txt like below.
# this is a comment details https://trac.ffmpeg.org/wiki/Concatenate
file 'E:\images\png\images__%3d.jpg'
file 'E:\images\jpg\images__%3d.jpg'
Sample usage as follows;
"h:\ffmpeg\ffmpeg.exe" -y -r 1/5 -f concat -safe 0 -i "E:\images\imagepaths.txt" -c:v libx264 -vf "fps=25,format=yuv420p" "e:\out.mp4"
-safe 0 parameter prevents Unsafe file name error
Related links
FFmpeg making a video from images placed in different folders
FFMPEG An Intermediate Guide/image sequence
Concatenate – FFmpeg
How to successfully use ffmpeg to convert images into videos
Your console output shows that you are writing 4:4:4 H.264 output, which very few players support. (Newer versions of FFmpeg warn about that.) Add -pix_fmt yuv420p
before the output file to convert to the widely supported 4:2:0 pixel format. You could additionally include -profile:v baseline
to make it work on even more players. (These options apply to encoding, so don't use them on the command that specifies -codec copy
.) You could also try playing your output file with ffplay
, which is one of the few players that support 4:4:4 H.264 in mp4 files.
If you want the same duration for each image and they are consecutively numbered, you only need a single command such as:
ffmpeg -framerate 0.2 -i %3d.jpg -vf fps=10 -pix_fmt yuv420p output.mp4
The -framerate 0.2
sets the input frame rate to 0.2 frames per second (1 frame every 5 seconds). %3d
is replaced by 3 digits 000
, 001
, etc. for each frame. -vf fps=10
will change the frame rate to 10 frames per second, in this case by duplicating input frames; it is not strictly necessary but can be helpful with low frame rates to make seeking smoother and ensure the full duration for the first and last images, and should not increase the output size by much since frames that are identical to the previous frame can be encoded in a small size.
If you want to continue using the concat
demuxer, or want different file names or durations for each image, you can specify the image directly with a duration for each one in your inputs.txt
. There is no need to create a separate movie file for each image. For example:
file 000.jpg
duration 5
file 001.jpg
duration 5
...
Then use the concat demuxer to concatenate them and perform the encoding at the same time:
ffmpeg -f concat -i inputs.txt -vf fps=10 -pix_fmt yuv420p output.mp4
If any of these things don't work, upgrade your FFmpeg to the latest version.
ffmpeg images to video with different start times and durations
This answer addresses the ffmpeg
specific questions in your broad multi-question.
This example will take images of any arbitrary size, fit them into a 1280x720 box, fade between images, and play the audio at the same time. The video will end whenever the images or audio ends: whichever is shortest.
ffmpeg \
-i audio1.mp3 \
-i audio2.wav \
-loop 1 -t 5 -i image1.jpg \
-loop 1 -t 5 -i image2.png \
-loop 1 -t 5 -i image3.jpg \
-filter_complex \
"[2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=out:st=4:d=1[v1]; \
[3:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v2]; \
[4:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v3]; \
[0:a][1:a]amerge=inputs=2[a];
[v1][v2][v3]concat=n=3:v=1:a=0,format=yuv420p[v]" \
-map "[v]" -map "[a]" -ac 2 -shortest -movflags +faststart output.mp4
How to concatenate two MP4 files using FFmpeg?
I ended up using mpg as the intermediate format and it worked (NOTE this is a dangerous example, -qscale 0 will re-encode the video...)
ffmpeg -i 1.mp4 -qscale 0 1.mpg
ffmpeg -i 2.mp4 -qscale 0 2.mpg
cat 1.mpg 2.mpg | ffmpeg -f mpeg -i - -qscale 0 -vcodec mpeg4 output.mp4
Related Topics
Linux: Get a Script to Be Able to Ask The User for a File Name Then Open That File
How to Remove Space/Tab from Command Output
Why Doesn't ''Var=Value Echo $Var'' Emit Value
Yocto: How to Install Header Files Along with Kernel Module in Sdk
Accessing Any Memory Locations Under Linux 2.6.X
Brother Ql-720Nw Specifying Media Size Seems Ignored
Hide Information During Bash Debug Run
How to Make a Cross Compiler Using Gcc
Restart Service from Cgi Script
How to Create a Folder with a Folder Name Containing Spaces in Linux
Why Do My Keystrokes Turn into Crazy Characters After I Dump a Bunch of Binary Data into My Terminal
Insecure $Env{Path} While Running with - T Switch
Search Ip from a Text File in .Csv Log File, If Found Add New Column Next to It
Bash Separate Parameters with Specific Delimiter
Problem in Restoring Floating Toolbar for Qmainwindow
(Mac) Leave Core File Where The Executable Is Instead of /Cores