nws_tools.img2vid

nws_tools.img2vid(imgpth, imgfmt, outfile, fps, filesize=None, ext='mp4', preset='veryslow')[source]

Convert a sequence of image files to a video using ffmpeg

Parameters:

imgpth : str

Path to image files

imgfmt : str

Format specifier for images. All files in the image stack have to follow the same naming convention, e.g., given the sequence im_01.png, im_02.png, ...,`im_99.png` the correct format specifier imgfmt is ‘im_%02d.png’

outfile : str

Filename (including path if not in current directory) for output video. If an extension is provided, e.g., ‘animation.mp4’ it is passed on to the x264 video encoder in ffmpeg to set the video format of the output. Use ffmpeg -formats in a shell to get a list of supported formats (any format labeled ‘Muxing supported’).

fps : int

Framerate of the video (number of frames per second)

filesize : float

Target size of video file in MB (Megabytes). If provided, a encoding bitrate will be chosen such that the target size filesize is not exceeded. If filesize = None the default constant rate factor of ffmpeg is used (the longer the movie, the larger the generated file).

ext : str

Extension of the video-file. If outfile does not have a filetype extension, then the default value of ext is used and an mp4 video is generated. Note: if outfile has an extension, then any value of ext will be ignored. Use ffmpeg -formats in a shell to get a list of supported formats (any format labeled ‘Muxing supported’).

preset : str

Video quality options for ffmpeg’s x264 encoder controlling the encoding speed to compression ratio. A slower preset results in better compression (higher quality per filesize) but longer encoding time. Available presets in ffmpeg are ‘ultrafast’, ‘superfast’, ‘veryfast’, ‘faster’, ‘fast’, ‘medium’, ‘slow’, ‘slower’, ‘veryslow’, and ‘placebo’.

Returns:

Nothing : None

Examples

Suppose the 600 sequentially numbered tiff files image_001.tiff, image_002.tiff, ..., image_600.tiff located in the directory `/path/to/images/ ` have to be converted to Quicktime movie (mov file) of no more than 25MB size. We want the video to show 6 consecutive images per second (i.e., a framerate of 6 frames per second). This can be done using the following command

>>> img2vid('/path/to/images','image_%03d.tiff','image_1_600',6,filesize=25,ext='mov',preset='veryslow')

Alternatively,

>>> img2vid('/path/to/images','image_%03d.tiff','image_1_600_loq.mov',6,filesize=25,ext='mkv',preset='ultrafast')

also generates an mov video of 25MB. The encoding will be faster but the image quality of image_1_600_loq.mov will be lower compared to image_1_600.mov generated by the first call. Note that the optional keyword argument ext=’mkv’ is ignored since the provided output filename ‘image_1_600_loq.mov’ already contains an extension.