LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Command Line Video Text Overlay (https://www.linuxquestions.org/questions/linux-software-2/command-line-video-text-overlay-650468/)

mundy 06-20-2008 01:29 AM

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

osor 06-20-2008 07:18 PM

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.

mundy 06-21-2008 05:23 AM

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?

osor 06-21-2008 11:51 AM

Quote:

Originally Posted by mundy (Post 3190952)
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?

With mencoder itself, I believe you can change things like font, color, style, etc. combined with somewhat crude vertical positioning (-subpos <0-100>).

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.

osor 06-21-2008 04:30 PM

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.

mundy 06-22-2008 08:57 PM

Quote:

Originally Posted by osor (Post 3191321)
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.

That looks like it's exactly what I'm after. I have ffmpeg installed on my system (Debian etch) but I'm unsure as to how to install these vhooks, or even to find out if they exist.

I tried calling ffmpeg with the -vhook option to see what happened:

Code:

$ ffmpeg -vhook 'vhook/imlib2.so'
<<ffmpeg version and usage output>>
vhook/imlib2.so: cannot open shared object file: No such file or directory
Failed to add video hook function: vhook/imlib2.so

I'm guessing I don't have the vhooks installed?

How do I install them so I can start using them? o_0

osor 06-22-2008 09:22 PM

Quote:

Originally Posted by mundy (Post 3192188)
That looks like it's exactly what I'm after.

I suppose you mean for “Part B” from your OP, since the vhooks are more for an overlay than for dynamic text (the timecode requirement of “Part A” could be met solely by use of hardcoded subtitles).
Quote:

Originally Posted by mundy (Post 3192188)
I have ffmpeg installed on my system (Debian etch) but I'm unsure as to how to install these vhooks, or even to find out if they exist.

I tried calling ffmpeg with the -vhook option to see what happened:

Code:

$ ffmpeg -vhook 'vhook/imlib2.so'
<<ffmpeg version and usage output>>
vhook/imlib2.so: cannot open shared object file: No such file or directory
Failed to add video hook function: vhook/imlib2.so

I'm guessing I don't have the vhooks installed?

How do I install them so I can start using them? o_0

I am not quite sure what to say since I don’t use Debian (on my system, the the file is /usr/lib/vhook/imlib2.so). You can try “locate imlib2.so” to see if that gives up any files (if so, you can specify the full path at the command-line).

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.

mundy 06-22-2008 10:20 PM

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
and here's the first few lines of my srt file:

Code:

1
00:00:00,000 --> 00:00:00,999
Q1 00:00

2
00:00:01,000 --> 00:00:01,999
Q1 00:01

3
00:00:02,000 --> 00:00:02,999
Q1 00:02

4
00:00:03,000 --> 00:00:03,999
Q1 00:03

5
00:00:04,000 --> 00:00:04,999
Q1 00:04


mundy 06-22-2008 10:42 PM

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

FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2007 Fabrice Bellard, et al.
  configuration: --prefix=/usr --libdir=${prefix}/lib --shlibdir=${prefix}/lib --incdir=${prefix}/include/ffmpeg --enable-shared --enable-libmp3lame --enable-gpl --enable-libfaad --mandir=${prefix}/share/man --enable-libvorbis --enable-pthreads --enable-libfaac --enable-xvid --enable-libdts --enable-amr_nb --enable-amr_wb --enable-pp --enable-libogg --enable-libgsm --enable-x264 --enable-liba52 --enable-libtheora --extra-cflags=-Wall -g -fPIC -DPIC --cc=ccache cc --enable-swscaler
  libavutil version: 49.4.0
  libavcodec version: 51.40.2
  libavformat version: 51.11.0
  built on Mar 29 2007 11:08:52, gcc: 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
Input #0, avi, from 'input.avi':
  Duration: 01:01:24.4, start: 0.000000, bitrate: 1767 kb/s
  Stream #0.0: Video: mpeg4, yuv420p, 640x480, 25.00 fps(r)
  Stream #0.1: Audio: adpcm_ima_wav, 32000 Hz, stereo, 257 kb/s

/usr/lib/vhook/imlib2.so: undefined symbol: exit_is_forbidden
Failed to add video hook function: /usr/lib/vhook/imlib2.so -c white -F VeraBd.ttf/12 -x 0 -y 0 -t %A-%D-%T

I've tried playing around with the options handed to imlib2.so, thinking perhaps the documentation was out of date but it seems no matter what I do the same error occurs.

I searched google with the error message but so far have only been able to find some gentoo specific help.

Any ideas?

osor 06-23-2008 02:52 PM

Quote:

Originally Posted by mundy (Post 3192225)
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

There is one of a couple problems here:
  1. Subtitles will not be hardcoded unless you are re-encoding the video (so you can’t expect “-ovc copy” to work). My advice is to keep all video parameters the same (you can use midentify in a script to get those parameters), and use “-oac copy” for the audio.
  2. Even when using a different ovc, I alluded to previously encountered bugs in older builds of mplayer in which subtitle hardcoding didn’t work (see this thread). So if the problem persists when you follow 1., I suggest getting a newer build of mplayer/mencoder (perhaps from the repository linked above).

osor 06-23-2008 02:57 PM

Quote:

Originally Posted by mundy (Post 3192231)
I'm also having a problem with ffmpeg and the imlib2.so vhook:
<SNIP>
I searched google with the error message but so far have only been able to find some gentoo specific help.

When I searched google with that error message, I came up with this hit.

P.S.,
You might also try the debian-multimedia repository I linked above.

mundy 06-23-2008 08:57 PM

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 =)

mundy 06-24-2008 01:08 AM

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
I've updated it to the latest version available, but still no luck =/

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?

osor 06-24-2008 08:48 PM

Quote:

Originally Posted by mundy (Post 3193312)
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
I've updated it to the latest version available, but still no luck =/

That would be version 1.0rc2? The above line works for me verbatim (on this test video, and the test srt file you provided) both on a Gentoo machine and an Ubuntu 8.04. Perhaps you should try from a testing or unstable repo. Or even compile the latest source.
Quote:

Originally Posted by mundy (Post 3193312)
As far as the ffmpeg vhook goes, I got it to work.

Well, this is good news. I agree that splitting up the file into one-second chunks is a horrible way to go.

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.

osor 06-24-2008 08:49 PM

Quote:

Originally Posted by mundy (Post 3193168)
Both MPlayer/MEncoder and FFMPEG were installed from the debian-multimedia repository in the first place.

Then maybe try the normal debian repository (unless you’ve already tried that).

osor 06-25-2008 08:56 PM

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
The above command works for me on the test xvid file I linked before (you have to have libgstxvid.so installed on your system), and you can customize the textoverlay further with options covered in the docs. In fact, you could do both the subtitle overlay as well as the image overlay all with gstreamer.

mundy 06-25-2008 09:27 PM

Quote:

Originally Posted by osor (Post 3195335)
Well… did you solve your problem?

Not yet, still trying things though. I didn't get much chance to look at it yesterday as I had other things to do.

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.