http://www.perlmonks.org?node_id=1181943

ozboomer has asked for the wisdom of the Perl Monks concerning the following question:

I have a Perl program that runs under both Windows (ActiveState v5.16.3 32-bit) and Linux (Puppy Linux v5.6.4 Slacko ... but others as well) which calls the video manipulation program 'ffmpeg' to modify the content of a video file. The current issue revolves around the adding of some text to the video image.

'ffmpeg' is called via `` (backticks) and system()... and in both cases, the executed command is something like:-

ffmpeg -hide_banner -i "scats.avi" \ -vf "fps=fps=pal, \ drawtext=fontsize=10: fontcolor=yellow: \ fontfile=arial.ttf: \ text='%{pts\:hms} Fr %{n}': x=8: y=h-(2*lh), \ scale=trunc(iw/2)*2:trunc(ih/2)*2 " \ -y -codec:v libx264 -pix_fmt yuv420p \ -crf 18 -vsync cfr -r pal "new.avi"

Note that this command is fairly generic and includes some "filter clauses" that are required for .flv files, .mp4 files, etc.

With the command as it stands, the font file "arial.ttf" must reside in the current directory... but I can fix that problem by using some perl code like:-

$my_os = "$^O"; if ($my_os eq "linux") { $font_file = "/usr/share/fonts/default/TTF/DejaVuSans.ttf"; } elsif ($my_os eq "MSWin32") { $dir_marker = "\\"; # $font_file = $ENV{"SystemRoot"} . $dir_marker . "Fonts" . $dir_m +arker . "Arial.ttf"; $font_file = "c\\:\\\\windows\\\\fonts\\\\verdana.ttf"; }

...and then use '$font_file' in the 'ffmpeg' command string.

The real problem comes from the situation where the TTF files don't exist. The files I've selected are fairly common... but is there a simple, robust, *portable* way to select some 'sans serif' -style fonts (font files) in Perl?

Some of what is done in HTML markup comes to mind... where a font is often declared in HTML or CSS files as 'sans serif' and the user's browser is set-up to translate 'sans serif' into a specified *actual* font. Is something like that available in a Perl module? ... or can I call the O/S (system32.dll or Linux equivalent)? I'm looking for the simplest solution here... and modules like Font::TTF are as clear as mud to me :(

I'd appreciate any thoughts on how to deal with this...

Thanks.

Edit: Note the spec for $font_file under MSWin32 as shown will not work. Still trying to find a spec that will...(!)

Edit: The pathing under Windows is horrendous... the 'normal' Perl internal magic that allows us to use '/' under Windows causes grief for 'ffmpeg'... and the 'ffmpeg' filtering items use '\' and ':' in a special way. *Ugh!* Note that the MSWin32 filespec shown *now* DOES work - it's just ugly.