Using ffmpeg

ffmpeg is a utility that can manipulate audio and video files in a large number of ways. For animators, its primary use is to take still images and generate a video file from them. There are some GUI video encoders that are a GUI front end bundled with ffmpeg, but ffmpeg is a command-line utility. I'll describe the command line options that I use when making my animations.
As I've said in earlier posts, I am not yet working with sound in my animations, and so I haven't explored the options required for including sound in a video file.
The command line
I use a .BAT file to run ffmpeg, to avoid having to type out the same command line over and over. Here is the command line that I use for creating the final animation from all of the sets of generated frame files:
"C:\Program Files\ffmpeg\bin\ffmpeg" -f concat -safe 0 -i framelist.txt -r 24 -y -c:v libx264 -pix_fmt yuv420p final.mp4
Here is what each term does, in order of its appearance in the command line:
"C:\Program Files\ffmpeg\bin\ffmpeg"
This is the full path to where I have ffmpeg installed. If your set-up is different you will need a different path. The path here is in quotes because Windows, for better or worse, allows spaces in file folder names.
-f concat
This tells ffmpeg that it will be concatenating several individual streams together.
-safe 0
This allows the framelist file to be used.
-i framelist.txt
This specifies the files used for import.
-r 24
This specifies the frame rate, in frames per second. 24 fps is the standard for movies (at least it was during the Golden Age of animation). You can set it slower or faster if you like.
-y
This causes ffmpeg to overwrite the output file if it is already present, without asking.
-c:v libx264
This specifies H.264 as the output format of the video stream. This format is recommended if you will be uploading your animations to YouTube.
-pix_fmt yuv420p
This specifies the color format for the output.
final.mp4
This specifies the output file name.
The file framelist.txt is the frame list file. The following is one example of what the file might contain:
file 's0000f%03d.png'
file 's0001f%03d.png'
Each of these lines tells ffmpeg to look for a series of frame files. The first series will have filenames starting with s0000f, followed by a three-digit number (starting at 000 and incrementing from there), and ending with .png. If there is a gap in the sequence of your files, ffmpeg will stop at the last file before the gap and move on to the next line in the frame list file.
A framelist file allows you to build your animation from shots of whatever length you choose, without having to worry about making sure that the files all have the same stem with a continuous sequence of frame numbers.
When I am coding a single sequence of files, all having the same stem and a continuous series of frame numbers, I can use a simpler command line:
"C:\Program Files\ffmpeg\bin\ffmpeg" -i test%%04d.png -y -r 24 -vcodec libx264 test.mp4
You will notice here that the -i option specifies the frames directly, instead of referring to the file frameset.txt. In this case the file stem is test and it is followed by a four-digit number, and not three-digit as before. The -safe option is only required when using a frameset file so it is not present here. You will also notice that the percent sign is repeated when the pattern specification is in the command line; the percent sign is a special character in MS-DOS command lines, but the special behavior is escaped by repeating it, and the doubled percent sign is reduced to a single percent sign when ffmpeg receives the parameter. When given these parameters, ffmpeg will begin with file test0000.png and continue until test9999.png or until it does not find a file in the sequence (whichever comes first).
Permission denied for the output file
Sometimes you will get a Permission denied error for the output file. This generally means one of two things:
  1. You do not have the proper permissions to write to the output directory.
  2. Your antivirus solution is blocking ffmpeg from writing the output file.
If the problem persists after verifying that you have write access to the output directory, then disabling your anti-virus solution long enough to run ffmpeg should solve the problem. (If the problem persists at this point, you'll have to seek help elsewhere, sorry.)

Comments

Popular posts from this blog

Transition Macros

Titling Made Simple

Star Field Generator