25/07/2008

Embedded movies in LaTeX PDF - the workaround

Not very elegant, but it works. Als o slightly historic: okular and acroread/flash can be made to play back movies to a certain extent, and lately I defected to exporting the PDF presentation to Impress, because it has a gorgeous presenter console (summary here).
Prerequisites: mplayer, acroread and a window manager capable of window rules: KWin, Compiz with CCSM or Gnome with Devil's Pie (I'm not a Gnome user, so no experience with the latter)
You can automate most of this with a shell script, but that's going to be another post (this is over-long already).


First, handle borderless display above the slides for mplayer:
Note: as an anonymous benefactor in the comments pointed out, borderless display can be got by simply passing the -noborder option to mplayer, so you can leave out some of the settings below:
KDE settings:
KControl (KDE4: Personal Settings) ->Desktop (KDE4:Window behaviour)->Window-specific settings->New...
In the "Window" tab, set Window class to "xv MPlayer" (or else, use the "Detect" or "Detect Window Properties" button and choose an existing mplayer window). In the Preferences tab, set checkboxes front and back for "Keep above" and "No borders" and change the drop-down option to "Force". The config file is ~/.kde/share/config/kwinrulesrc, keep a backup copy if you want to switch back between normal and presentation settings.
Compiz settings:
In CCSM->Window rules, set class=MPlayer in Above and class=Acroread in Below (else the movie window will start behind the full screen slides. You will have to revert these settings for normal use again.) In CCSM -> Window decorations, add & !(class=MPlayer) for both Decoration Windows and Shadow Windows. You can switch between these and normal settings with a script.
I don't have Gnome, which apparently has no native window rules setting, so you have to figure out the Devil's Pie syntax yourself (this thread might help).

Latex code and shell script:
Edit ~/.mailcap, add the line application/x-shellscript;/bin/bash %s
I coudn't find mailcap in openSuSE 11; try Kcontrol->KDE Components->File associations.
You don't embed the movie, but an external shell script starting mplayer. This is why we have to use acroread. Kpdf disables executables for security reasons; maybe it is possible to define exceptions somewhere, but I didn't check. In the latex source code, use the following:
\href{run:movie.sh}{\includegraphics[width=<width on slide>]{<movie still>.jpg}}
Create the shell script movie.sh:
#!/bin/bash
mplayer -geometry [-noborder] <xoffset(% or px)>:<yoffset(% or px)> -vf scale -xy <width on slide in px> -loop 0 <the movie>.avi

In theory, any mplayer-compatible movie format should work also. Make the script executable.
Fiddle with the offset/width settings in the script until the movie window is exactly in place of the still image. Note that the settings change with screen resolution, so set your screen to the resolution of the projector you are going to use.
To get this working, run acroread <the TeXfile>.pdf in your latex source dir from a shell (we used relative paths), go to fullscreen (Ctrl+L). The movie should start on click.

Didn't say it was elegant, did I?
On the other hand, if you are not interested in simulating an embedded movie, you can skip the stuff about no window borders and exact positioning.

6 comments:

Anonymous said...

Thank you, thank you, thank you!

avocadohead said...

You're very welcome ;-)

Anonymous said...

That did help a lop, thx!
And, btw, there is the "-noborder"-Option for mplayer, this way you don't need to get compiz to work.

avocadohead said...

Thanks for the tip - I I have reworked the WM section accordingly

ga said...

Hello,

I'm trying to make it work under ubuntu 9.04 but I failed.

I created the

~/.mailcap
movie.sh

files. I can run

./movie.sh

and see my avi or mpg files.

I have my pdf created with beamer and I have added the command

\href{run:movie.sh}{\includegraphics[width=66mm]{myfile.eps}}

However, the command

acroread filename.avi

returns an error (unsupported or damaged file).

If I try as well to open the pdf when I click on the image the file movie.sh is opened with a text editor (!).

Any suggestion?

Thanks,
g.

avocadohead said...

Sorry, must have overlooked that last comment - kind of coincided with both my finals and moving away…
acroread xxx.avi was a typo (sorry, my bad), I meant the PDF file.
If the shell file is opened instead of executed, there are two possiblities:
a) it's not executable (did you do chmod a+x xxx.sh?) and treated like a text file
b) MIME types are not properly set. I'm sorry, but I'm not too familiar with Gnome/Ubuntu. Maybe ~/.mailcap is not recognised - the system-wide mailcap file is in /etc. You might try editing that one, but I'd recommend reading the mailcap man page before that.

Good luck

avocadohead