Command Line Video Text Overlay
Presently I have a working system of scripts that uses MEncoder to cut and join video files together with a handful of other options etc. It's all automated and working and that's just dandy.
What I'm looking for now is a way to place text over the video using the scripts. This would be done in a similar way to subtitles, except the subtitles would be embedded in the video (so each frame actually includes the text as part of the actual frame itself). Is there a software package/library that does this via the command line? or in some other way that can be easily managed with scripts? Does this functionality already exist in MEncoder? I'll give you a few examples of what I'm after. Basically I have a script that reads in a handful of timecodes and a video file from a database, invokes mencoder to cut the specified clips from the source videos, then uses mencoder to join them all into a single file. What I want to be able to do is: a) Provide a timestamp on the video that would obviously update each second (this could all be calculated by the script) b) Take some extra text from the database, along with the clip timecodes and overlay it onto the specific clip None of this seems like it would be particularly hard to do, software wise, but i've been searching and searching for a software library that can do it with little luck. Anyone have any ideas? I'd be willing to PayPal somebody like $50 AUD if they can point me in the right direction o_0 |
There are a few ways you could go about doing this. For example, use ImageMagick on each frame separately. But I think you already hit on the easiest way—using subtitles.
Certain formats of subtitle files are easy to generate programatically (e.g., srt). Generally, if the subtitle and movie file are in the same directory (with the same basename), mplayer will automatically use the subtitle file and overlay the text (with some adjustable font and style parameters). When using mencoder, on the other hand, you can use this behavior to “hardcode” the subtitles onto the resulting movie file using the “-sub” flag (although I think there were certain bugs with this usage on older builds of mencoder). Here is an example usage. So I think the steps are to have a script or program (I would use Perl) to generate a text-based subtitle file with the text you want (using the format shown in the linked-to wiki page). Then, use mencoder to hardcode the subtitle file onto a single resultant video file. Edit: P.S. If you were serious about the PayPal, send me a personal message through email, and we could discuss that further. |
Using the subtitle method, would I be able to change things like position on screen, background colours etc? I'm assuming using the subtitle method I'd be limited to just text too?
|
Quote:
But of course mencoder is not the only software capable of “flattening” a subtitle onto a video. If you want something more robust, take a look at spumux from the dvdauthor package. I am sure there is other software out there which has similar features. |
I forgot to mention that instead of subtitles you can use mencoder’s cousin—ffmpeg, which has all sorts of plugins (called vhooks).
If you have the correct build of ffmpeg, there are two vhooks you may be interested in: drawtext and imlib2 (for plain and fancy text respectively). You can also use the ppm and watermark vhooks for overlaying images. Here is the documentation for using the hooks. |
Quote:
I tried calling ffmpeg with the -vhook option to see what happened: Code:
$ ffmpeg -vhook 'vhook/imlib2.so' How do I install them so I can start using them? o_0 |
Quote:
Quote:
Perhaps you could try the ffmpeg package provided by debian-multimedia (whose manifest shows the so-files in the correct location). If none of this works, you could build from scratch. |
locate imlib2.so returned /usr/lib/vhook/imlib2.so which obviously means it's on my system. I'm currently fiddling with it now, I'll let you know how I go.
As for the subtitle embedding with mencoder, I've put together a .srt file and used the -sub option in mencoder, but the resulting file returns with no subtitles at all. Heres the options i used: Code:
$ mencoder "input.avi" -ovc copy -oac copy -sub subs.srt -o output.avi Code:
1 |
I'm also having a problem with ffmpeg and the imlib2.so vhook:
(using an example from the documentation page) Code:
$ ffmpeg -i "input.avi" -vhook '/usr/lib/vhook/imlib2.so -c white -F VeraBd.ttf/12 -x 0 -y 0 -t %A-%D-%T' output.avi I searched google with the error message but so far have only been able to find some gentoo specific help. Any ideas? |
Quote:
|
Quote:
P.S., You might also try the debian-multimedia repository I linked above. |
Both MPlayer/MEncoder and FFMPEG were installed from the debian-multimedia repository in the first place. I'll do an upgrade and see if that helps the vhook problems.
I'll give the subtitles another go with your advice too and let you know how I go. Thanks =) |
Still unable to get mencoder to embed the subtitles. I'm re-encoding the video and the path the the subs.srt file is fine but still not having any luck.
Code:
$ mencoder "input.avi" -oac copy -ovc xvid -xvidencopts bitrate=1500 -sub subs.srt -o output.avi As far as the ffmpeg vhook goes, I got it to work. As the vhook only allows static text overlays, I had to get my script to generate a separate command for each second which would create a 1 second long clip for each clip. This works... It's just not every practical to break the clips down further into 1 second clips and then join them again. Anyone have any other ideas? |
Quote:
Quote:
If you still can’t get mencoder to work for you, you could always use something else for hardsubbing. Does spumux not work since you’re dealing with xvid? Another option is vlc, which can probably do hardcoding of subtitles, but I am not sure of its script-friendliness. Also, there may be a way to interface with a library like gstreamer, and write the overlay code as part of a program/script. |
Quote:
|
Well… did you solve your problem?
I am not very well-acquainted with gstreamer, but I found that you can also use it to hardcode subtitles as well. Gstreamer uses a sort of pipeline to apply filters, and takes some getting used to. You can use the command-line gst-launch to set up such a pipeline if you don’t want to bother with a Python or Perl script: Code:
gst-launch filesrc location=subs.srt ! subparse ! txt. filesrc location=input.avi ! decodebin ! textoverlay name=txt ! xvidenc ! filesink location=output.avi |
Quote:
Not sure whats up with MEncoder not working. FFMPEG and imlib2.so works but isn't terribly practical so I'll see what else I can find. I'll try and have a play with gstreamer today and see what I come up with. |
All times are GMT -5. The time now is 01:49 AM. |