Transcoding using FFmpeg
This document will show how to set up transcode using FFmpeg controlled by the CatDV Worker.
FFmpeg is a powerful, open source, cross platform encoder. It’s home is here. There are several pre-built FFmpeg distributions available for Mac and Windows (here) or it can be compiled yourself (which can be useful if you want to use third party extensions to FFmpeg, for example to burn in text or subtitles). There is a good tutorial for installing FFmpeg with extensions on Mac here and for Windows this site offers custom ffmpeg builds on request.
This tutorial is based on OS X installed as above, with FFmpeg 2.1.3 and extensions allowing text burn in.
First Check FFmpeg is installed and available
First open a Terminal window and check FFmpeg can run, plus note where the program is installed. For this installation it is here :
"/opt/local/bin/ffmpeg"
In a terminal window, run ffmpeg, i.e. type the command above. If FFmpeg is installed correctly you should see the version number and other configuration information.
Set up a Worker Watch action
Now Navigate to the CatDV Worker, “Edit Config” and add a new action (the plus button).
We want to give our action a name, triggered by a file, and set up a watch action wherever the footage will arrive, for example:
Next we can choose which media this action should work with. In this case “All known media types” are selected. Now on the “Conversions” tab we will tell the Worker to run ffmpeg. There are many options for ffmpeg including formats, frame sizes, watermarks, burn ins etc. The reader is encouraged to explore these settings for their workflow. In this case we shall transcode to H.264 for the web.
In the “Execute Command 1” box type the command line to run ffmpeg, something like:
/opt/local/bin/ffmpeg -i $b -codec:v libx264 -movflags +faststart -preset slow -crf 30 -vf scale=320:-1 -codec:a aac -strict -2 -vf "[in]drawtext=fontfile=/Library/Fonts/Arial.ttf: timecode='09\:57\:00\:00': r=25: \x=(w-tw)/2: y=h-(2*lh): fontcolor=white: fontsize=32: box=1: boxcolor=0x00000000@1, drawtext=fontfile=/Library/Fonts/Arial.ttf: \text='Burn In Text': \x=(w-tw)/2: y=(2*lh): fontsize=32: fontcolor=red: box=1: boxcolor=0x00000000@1[out]” "/Volumes/NAB/Worker Webinar Outputs/ffmpeg Transcodes/$g.mp4"
Breaking down this command line:
/opt/local/bin/ffmpeg | The full file path to the ffmpeg executable |
-i $b | Sets the input file to be a CatDV variable $b, the full input file path. A full description of CatDV variables that can be used in the command line is here. |
–codec:v libx264 | Tells ffmpeg to use H.264 encoding for video (v) |
-movflags +faststart | Makes the resulting file streamable for web viewing |
-preset slow -crf 22 | Sets the quality of the H.264, crf of 18 is intended to be visually lossless, higher numbers mean faster encodes, but lower quality. There are some good notes on the settings required to create high quality H.264 encodes here |
-vf scale=320:-1 | Sets the width of the clip to 320, -1 for the height keeps the aspect ration the same as the source clip. Several variables are supported in i.e. h/2 is half the height of the original clip. There’s good information on resizing with ffmpeg here. |
-codec:a aac -strict -2 | Sets the audio format to be arc, using the built in aac encoder. This is a fairly low quality encoder but is bundled with most pre-built ffmpeg distributions. Other audio encoders are available, see here for other options. |
-vf “[in]drawtext=…….[out]” | This burns in some text and a time code, the detailed syntax is described here. In summary for our example we have two separate pieces of burnt in text, i.e. two ‘drawtext=‘ statements separated by a comma. Each drawtext statement has items separated by colons: |
fontfile=/Library/Fonts/Arial.ttf: | Sets the font |
fontcolor=white: | Sets the font colour |
fontsize=20: | Sets the font size |
box=1: boxcolor=0x00000000@1 | Draws a filled in box of a particular colour round the text |
\text=’Burn In Text’: | Writes the static text ‘Burn In Text’ |
timecode=’09\:57\:00\:00’: | Writes the timecode with a leading zeros |
\x=(w-tw)/2: y=h-(2*lh): | Sets the x,y position for the text starting from top left. In this case centring the text horizontally and placing the a line up from the bottom of the clip. Variables or numbers can be used, ‘w’ is clip width, ‘tw’ is text width, ‘h’ is clip height, ‘lh’ is line height |
“/Volumes/NAB/Worker Webinar Outputs/ffmpeg Transcodes/$g.mp4” | Determines where to write the output file. In this case we are using another CatDV variable: $g is the file name (i.e. with none of its path) and without any file extension. This means all output files we create will have the mp4 extension. |
There are many other options provided by FFmpeg which can be useful in real world workflows, for example -y and -n control whether files are overwritten or not.
In this step we can also move the original inbound footage to our SAN:
and publish to a CatDV Catalog:
Now save the Worker action and return to the main Worker queue.
Running the Worker Action
The worker watch action picks up an inbound file, processes it then starts the FFmpeg transcode. The worker takes status from FFmpeg and displays progress it as it processes the file:
Here’s a screen shot of the resulting movie:
There is more information on Troubleshooting Worker actions that use the Execute Command capability here.