When fms2 is used as a Streaming Media Server, You need to convert all the videos uploaded by all users into the FLV format. This format has a small file capacity and is suitable for remote playback. FFMPEG and mencoder are enough to convert most videos into FLV.
Here, I use Perl to call FFMPEG and mecoder commands, Java to operate databases and control threads to call Perl for conversion. I can't tell you clearly. Let's talk about the source code directly:
1. Covert. pl
#! /Usr/bin/perl
My $ encoder = $ argv [0]; // parameters passed in Java, encoding commands, such as:/usr/local/ffmepg
My $ filein = $ argv [1]; // The source file path to be converted for the parameters passed in Java
My $ fileout = $ argv [2]; // The converted file path for the parameters passed in Java
My $ logpath = $ argv [3]; // parameters passed in Java, Log Path
My $ localtime = localtime; // current time
My $ cmd;
My $ coder = substr ($ encoder, rindex ($ encoder, "/") + 1 );
If ($ coder EQ "FFMPEG "){
$ Cmd = $ encoder. "-I ". $ filein. "-AB 64-acodec MP3-Ac 1-ar 22050-B 230-r 29.97-y ". $ fileout; // If the conversion command is FFMPEG, call this command to convert
}
Else {// otherwise, call this command to use FFMPEG for conversion
$ Cmd = $ encoder. "-Of lavf-lavfopts
I _certify_that_my_video_stream_does_not_use_ B _frames-OVC lavc
-Lavcopts vcodec = FLV: vbitrate = 500-srate 22050-OAC lavc-lavcopts
Acodec = MP3: abitrate = 56-ffourcc flv1-OAC mp3lame ". $ filein."-o
". $ Fileout;
}
'Echo $ localtime: $ cmd> $ logpath'; // write the command content to the log
'$ Cmd'; // execute the conversion command
2. covertvideo. Java
FFmpeg conversion FLV:
Public synchronized static Boolean ffmpegtoflv (string filein, string fileout ){
Simpledateformat format = new simpledateformat ("yyyy-mm-dd ");
String response file = configuration. getinstance () // obtain the path of the perl file from the configuration file
. Getconfigvalue ("path. Perl ")
+ "Covert. pl ";
String encoder = configuration. getinstance (). getconfigvalue (// obtain the command line path from the configuration file
"Commend. FFMPEG ");
String logpath = configuration. getinstance (). getconfigvalue (// Log Path
"Stdout. Path ")
+ Format. Format (new date () + ". txt ";
Stringbuffer cmd = new stringbuffer ("/usr/bin/Perl"). append (writable file)
. Append (""). append (encoder). append (""). append (filein). append (
""). Append (fileout). append (""). append (logpath );
System. Out. Print (CMD. tostring ());
String line = NULL;
Inputstream stderr = NULL;
Inputstreamreader ISR = NULL;
Bufferedreader BR = NULL;
Runtime RT = NULL;
Boolean success = false;
Try {
RT = runtime. getruntime ();
Process proc = rt.exe C (CMD. tostring (); // execute the command and call Perl for conversion
Stderr = Proc. geterrorstream ();
ISR = new inputstreamreader (stderr );
BR = new bufferedreader (ISR );
System. Out. println ("<ffmpegtoflv> ");
While (line = Br. Readline ())! = NULL)
System. Out. println (line );
System. Out. println ("</ffmpegtoflv> ");
Int exitval = Proc. waitfor ();
System. Out. println ("process exitvalue:" + exitval );
File filepath = new file (fileout );
// If the object exists and the length is not 0, the conversion is successful.
Success = filepath. exists () & filepath. Length ()> 0;
} Catch (throwable t ){
T. printstacktrace ();
} Finally {
Try {
Stderr. Close ();
ISR. Close ();
BR. Close ();
} Catch (exception e ){
E. printstacktrace ();
Rt. Exit (1 );
}
}
Return success;
}
The conversion from mencoder to FLV is almost the same as above. Do not write comments:
Public synchronized static Boolean mencodertoflv (string filein,
String fileout ){
Simpledateformat format = new simpledateformat ("yyyy-mm-dd ");
String response file = configuration. getinstance ()
. Getconfigvalue ("path. Perl ")
+ "Covert. pl ";
String encoder = configuration. getinstance (). getconfigvalue (
"Commend. mencoder ");
String logpath = configuration. getinstance (). getconfigvalue (
"Stdout. Path ")
+ Format. Format (new date () + ". txt ";
Stringbuffer cmd = new stringbuffer ("/usr/bin/Perl"). append (writable file)
. Append (""). append (encoder). append (""). append (filein). append (
""). Append (fileout). append (""). append (logpath );
System. Out. Print (CMD. tostring ());
String line = NULL;
Inputstream stderr = NULL;
Inputstreamreader ISR = NULL;
Bufferedreader BR = NULL;
Runtime RT = NULL;
Boolean success = false;
Try {
RT = runtime. getruntime ();
Process proc = rt.exe C (CMD. tostring ());
Stderr = Proc. geterrorstream ();
ISR = new inputstreamreader (stderr );
BR = new bufferedreader (ISR );
System. Out. println ("<mencodertoflv> ");
While (line = Br. Readline ())! = NULL)
System. Out. println (line );
System. Out. println ("</mencodertoflv> ");
Int exitval = Proc. waitfor ();
System. Out. println ("process exitvalue:" + exitval );
File filepath = new file (fileout );
// If the object exists and the length is not 0, the conversion is successful.
Success = filepath. exists () & filepath. Length ()> 0;
} Catch (throwable t ){
T. printstacktrace ();
} Finally {
Try {
Stderr. Close ();
ISR. Close ();
BR. Close ();
} Catch (exception e ){
E. printstacktrace ();
Rt. Exit (1 );
}
}
Return success;
}
the configuration file config. properties needs to be placed in the Web Service's WEB-INF/classes directory, just according to the actual situation to change the path in the configuration file to your own can be used.