ffmpeg: Difference between revisions
Line 40: | Line 40: | ||
The following code snippet outlines how to call ffmpeg from within Python. | The following code snippet outlines how to call ffmpeg from within Python. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python" line> | ||
import subprocess | import subprocess | ||
mov_fps = 12 | mov_fps = 12 |
Revision as of 13:36, 27 April 2018
FFmpeg is a useful program for stitching together separate image files into a movie file. In particular, this is useful for producing movies of simulation. If you create a series of image files (png, mpeg, etc) for your simulation outputs, ffmpeg can then combine them into a movie file (mp4, avi, etc).
Of course, ffmpeg can do quite a bit more, and is actually a pretty powerful tool, but here we will focus on converting a collection of images into a movie.
Usage / Example
Suppose you have a directory frames
containing files frame_0000.png, frame_0001.png, frame_0002.png, ...
and want to make movie.mp4
.
ffmpeg -framerate 12 -r 12 -i frames/frame_%04d.png -q 1 -pix_fmt yuv420p -vf scale=-1:600 movie.mp4
In the above line:
-framerate 12 -r 12
sets the frame rate is set to 12 frames per second-i frames/frame_%04d.png
sets the input (-i) to the the png files.%04d
means that the file names have four-digit integers.-q 1
tells ffmpeg to maintain full image quality-pix_fmt yuv420p
sets the image formatmovie.mp4
specifies the name of the output file-vf scale=-1:600
re-scales the images to be 600 pixels tall, while maintaining the aspect ratio.
Calling within MATLAB
The following code snippet outlines how to call ffmpeg from within MATLAB.
frame rate = 3;
first_out = 0;
filename = ‘name_of_file';
status = system(['ffmpeg -r ',num2str(framerate),...
' -start_number ',num2str(first_out),' -i tmp_%03d.png',...
' -r ',num2str(framerate),' -y -pix_fmt yuv420p -q 1',...
' -vf scale=-1:600 ../movies/',filename,'.mp4']);
if status ~= 0
disp([filename,'.mp4 was possibly not rendered correctly.'])
end
Calling within Python
The following code snippet outlines how to call ffmpeg from within Python.
import subprocess
mov_fps = 12
in_name = 'frames/frame_%04d.png'
out_name = 'movie.mp4'
cmd = ['ffmpeg', '-framerate', str(mov_fps), '-r', str(mov_fps),
'-i', in_name, '-y', '-q', '1', '-pix_fmt', 'yuv420p',
'-vf', 'scale=-1:600', out_name]
subprocess.call(cmd)