27/07/2008

Automating the pdf movie workaround

These ought to make the PDF-embedded movie workaround a bit less cumbersome.

Shell script to change compiz settings:
First, set compiz (flat-file config) to the presentation-style settings (MPlayer above and no decorations, Acroread below) and copy the ~/.config/compiz/compizconfig/Advanced.ini (or whatever profile you are using. If in doubt, check CCSM->Preferences) to Advanced.ini.beamer - should include something like the following sections:
[decoration]
as_command = emerald --replace
as_decoration_match = (any) & !(class=MPlayer)
as_shadow_match = (any) & !(class=MPlayer)& !(class=Kicker) & !(class=Katapult)

[winrules]
s0_below_match = class=Acroread
s0_above_match = class=MPlayer
Revert your settings. To automatically set to beamer-mode, execute the following script:
#!/bin/bash
cd ~/.config/compiz/compizconfig
cp Advanced.ini Advanced.ini.backup
cp Advanced.ini.beamer Advanced.ini
To revert:
#!/bin/bash
cd ~/.config/compiz/compizconfig
cp Advanced.ini.backup Advanced.ini
No compiz restart required.
With kwin, you can do the same for ~/.kde/share/config/kwinrulesrc, add the section
[<rulenumber>]
Description=Settings for xv MPlayer
above=true
aboverule=2
noborder=true
noborderrule=2
title=MPlayer
titlematch=0
types=1
wmclass=xv mplayer
wmclasscomplete=true
wmclassmatch=1

You will probably have to log out and in to reload the configuration.

Python script to create the mplayer shell script with the appropriate scalings (extract them from the PDF file):
#!/usr/bin/python
screen=[1024.0,768.0]
f=open("filename.pdf", "r")
read=f.read()
mb=read.find("/MediaBox") #look for page bounding box "[x,y]"
mb1=read.find("[", mb)
mb2=read.find("]", mb)
pgbb=read[mb1+1:mb2].split(" ")
#reverse search for movie placeholder image bounding box
a=read[::-1].find("moviename.sh"[::-1])
b=read[::-1].find("]",a)
c=read[::-1].find("[",a)
imgbb=read[::-1][b+1:c][::-1] .split(" ")
x0=float(imgbb[0])*screen[0]/float(pgbb[2])
xwidth=(float(imgbb[2])-float(imgbb[0]))*screen[0]/float(pgbb[2])
y0=screen[1]-float(imgbb[1])*screen[1]/float(pgbb[3]) # TODO: include black border!
shell=open("moviename.sh", "w")
shell.write("#!/bin/bash"+"\n"+"mplayer -geometry %(x)d:%(y)d -vf scale -xy %(xwidth)d -loop 0 moviename.avi" % {"x":x0, "y":y0, "xwidth":xwidth})
shell.close()

I did a simple wx-GUI (which for unknown reasons has encoding problems with some PDF files - the above script hasn't). Compared to a presentation screenshot the coordinates can be off by a few pixels, so nitpickers like me had better tune the shell script a bit afterwards.

No comments: