Automatically creating videos from pictures, music and subtitles

So for one of my projects we have a number of albums and individual songs which we want to upload to youtube as many people use this to listen to music these days. We also want to create a separate collection of videos that have the song words (Think hard-burning subtitles into a video). Obviously you can do this in video editing software but it would be nice to be able to tweak all the videos afterwards without having to do much work.

Initially I tried using avconv/mencoder to generate videos based on the pictures using the following code – generate the picture/music as a video, apply subtitles and then finally apply the audio again but without reencoding it.

Whilst this kind of works it’s got a number of downsides the big ones being 1) it isn’t flexible to eg add another picture/slide at the end, and 2) it reencodes the video/audio a number of times.

Then I remembered that the great kdenlive video editing software is actually just a frontend to the brilliant mlt framework. This is basically a library plus commandline programs to do all sorts of video mixing with live or rendered output.

Using the melt commandline program you can test and generate tracks without having to worry about the XML format that it typically uses for the more advanced options. The final commands:

If you want to do the video output you can add the following onto the last command:

Lets go through this a line at a time:

Generate black background for 5614 frames

Followed by t.jpg for 250 frames

Generate a new track which is the cd image for the same length as the black track

Mix the two tracks so that the second one (ie the cd image) is 70% of the screen height and centered horizontally to the top.

Output to an xml file (in order to apply subtitles to the whole thing we need to do this intermediary stage)

Start with the mixed video sequence defined in the xml file (which is just instructions, not a staged render)

Apply the watermark filter with a subtitle mpl file, align to the bottom centered (it will auto scale extra wide lines to be the width of the video). A MPL file looks like this:

Where the first bit is the frame and the second bit is any text to be displayed. New lines demarcated with a tilde (~) character. Here is a simple perl script to convert a srt format subtitle file into this mpl format:

Back to the melt commandline:

Overlay the audio track

For the non-test output commandline parts:

Output using libav

Set the profile to be 25fps 720p hd video using mpeg, set audio rate 96kbps and video rate 1000kbps

Leave a Reply

Your email address will not be published. Required fields are marked *