API Reference >> skvideo.io.FFmpegWriter
Fork me on GitHub

skvideo.io.FFmpegWriter

class skvideo.io.FFmpegWriter(filename, inputdict=None, outputdict=None, audiosrc=None, verbosity=0)[source]

Writes frames using FFmpeg

Using FFmpeg as a backend, this class provides sane initializations for the default case.

Parameters:
filenamestring

Video file path for writing.

inputdictdict

How to interpret the frames piped in from Python.

outputdictdict

How to encode the output file.

audiosrcstring, optional

Path to a media file whose audio track should be muxed into the output. Used to preserve audio across a vread / vwrite round-trip (issues #173, #176). By default the output contains the video from the piped-in frames plus the first audio stream from audiosrc (equivalent to passing -map 0:v:0 -map 1:a:0 to ffmpeg).

To take full control of stream selection, supply your own -map in outputdict. When outputdict contains a -map entry, our default -map arguments yield entirely; this is in line with ffmpeg’s additive -map semantics (a second -map adds streams rather than overriding earlier ones). Examples (note the list form for multiple -map values):

  • Copy all audio streams:

    outputdict={'-map': ['0:v:0', '1:a']}
    
  • Pick a specific audio stream:

    outputdict={'-map': ['0:v:0', '1:a:5']}
    

The audio is stream-copied with -c:a copy (no re-encoding); set -c:a (or -codec:a / -acodec) in outputdict to override the codec. The output is also trimmed to the shorter of video/audio via -shortest, which is the intuitive behavior when the video is a subset of the original.

verbosityint

0 (default) for quiet, 1 to print the ffmpeg command.

Methods

close()

Closes the video and terminates FFmpeg process

writeFrame(im)

Sends ndarray frames to FFmpeg

Notes

The FFmpeg subprocess is launched lazily on the first writeFrame call. Constructing a writer and calling close() without writing any frames is a no-op and produces no output file — this keeps cleanup in a finally (or a construct-only validation) from raising. Use the high-level skvideo.io.vwrite, which rejects empty (0-frame) input with ValueError, if you want that guarded.

__init__(filename, inputdict=None, outputdict=None, audiosrc=None, verbosity=0)[source]

Prepares parameters

Does not instantiate the an FFmpeg subprocess, but simply prepares the required parameters.

Parameters:
filenamestring

Video file path for writing

inputdictdict

Input dictionary parameters, i.e. how to interpret the data coming from python.

outputdictdict

Output dictionary parameters, i.e. how to encode the data when writing to file.

Returns:
none
close()

Closes the video and terminates FFmpeg process

Raises RuntimeError if FFmpeg exited with a non-zero return code, including its stderr output in the exception message (issue #111). Previously a failing encode produced a silent empty/corrupt output file with no indication of what went wrong.

writeFrame(im)

Sends ndarray frames to FFmpeg