[LAU] A webserver with on-the-fly format conversion?

Rob lau at kudla.org
Thu Aug 13 00:15:25 EDT 2009

On Tuesday 11 August 2009 03:31 pm, Dominic Sacré wrote:
> Is there any way to set up a webserver that will automatically convert
> the various audio file formats to MP3?

Sure, if I could write a CGI script that shelled out to an ffmpeg script 
and converted videos to flv on the fly to watch in my Wii browser using 
Flowplayer, converting to mp3 on the fly ought to be cake.  Here's what I 
wrote (word wrap will probably break up comments and make them not run):


# youtube resolution is 320x240, but wii browser works better at 480x348
time ffmpeg -i "$@" -s 468x320 -b 500k -r 24 -f flv -ar 22050 -


use CGI qw(:standard);
use strict;
my $videodir = "$ENV{HOME}/Video/music-videos/need-re-encoding";

if ($ENV{PATH_INFO} =~ /\./) {
    $SIG{HUP} = sub { warn("$0: got a HUP\n"); exit 0;}; # kill our child 
ffmpeg process.... i hope 
    $SIG{TERM} = sub { warn("$0: got a TERM\n"); exit 0;}; # kill our child 
ffmpeg process.... i hope 
    $SIG{PIPE} = sub { warn("$0: got a PIPE\n"); exit 0;}; # kill our child 
ffmpeg process.... i hope 
    my $path = $ENV{PATH_INFO};
    $path =~ s|.*/||g;
    $path =~ s|\.flv$||;
    die "$videodir/$path doesn't exist" unless -f "$videodir/$path";
    print header("video/x-flv");
    open IN, "./streamasflv \"$videodir/$path\" |";
    my $data;
    while (read IN, $data, 4096) {
	print $data;
    close IN;
    exit 0;

my $curvid;
if (param("video") ne '') {
    $curvid = param("video");

opendir VIDS, "$videodir";
my @vids = readdir(VIDS);
closedir VIDS;

my $vidlist;
my $i;
for my $vid (@vids) {
    next unless -f "$videodir/$vid";
    $curvid = $vid unless $curvid;
    my $bg;
    if ($i++ % 2) {
	$bg = "white";
    } else {
	$bg = "#c0c0ff";

    $vidlist .= qq|<div style="width: 800px; background: $bg;"><a 

print header("text/html");
print qq|<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="swfobject.js"></script>
<body bgcolor="white">

<div id="flowplayerholder">
	This will be replaced by the player. 
<script type="text/javascript">
// <![CDATA[
var fo = new SWFObject("../FlowPlayerLight.swf", "FlowPlayer", "468", 
"350", "7", "#ffffff", true);
// need this next line for local testing, it's optional if your swf is on 
the same domain as your html page
fo.addParam("allowScriptAccess", "always");
fo.addVariable("config", "{ countryCode: 'fi', playList: [ {overlayId: 
'play' }, { url: '$ENV{SCRIPT_NAME}/$curvid\.flv' } ], initialScale: 
'scale',  fullScreenScriptURL: 'fullscreen.js' }");
// ]]>


# end of test.cgi

I had another script that started streaming requested videos while 
simultaneously queueing them for non-realtime encoding in the background, 
providing better quality and allowing the user to seek within the video, 
but it was kind of flaky and I never got it to where I wanted it and after 
moving to a new place, never really had an occasion to watch video on the 
Wii.  For instant gratification and transparency, the above worked fine.


More information about the Linux-audio-user mailing list