Build fms2 Streaming Media Server in Linux 4-format conversion

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.