Use FFMPEG to convert the video format to FLV to ensure successful operation

Source: Internet
Author: User
Tags flv file
/*** Video conversion (converted to FLV format) ** Creation Time: 2010-11-21 */public class testconvert {/** input file path attribute */private string in_path; /** output file path attribute */private string out_path;/** ffmpeg.exe storage path attribute */private string ffmpeg_path;/** completion progress attribute */private int complete = 0; /** constructor */Public testconvert (string inpath, string outpath, string ffmpegpath) {This. in_path = inpath; // value the path of the video file to be converted. This. out_path = outpath; // The FLV file path generated by the value assignment conversion. This. ffmpeg_path = ffmpegpath; // assign a value to the external Program Directory where FFmpeg is located}/** main method (TEST) */public static void main (string [] ARGs) {testconvert convert = new testconvert ("E: // input //. MP4 "," E: // output // W. FLV "," E: // "); convert.exe cute (convert. toflv (); // Method for converting to FLV and adding a watermark // convert.exe cute (convert. getimage ();}/** Conversion Method */Public void execute (list <string> List) {processbuilder builder = new processbuilder (); // process builder object builder. command (list); // create a life cycle with dynamic array object parameters Make try {process Pc = builder. start (); // The thread starts the execution. // call the readinputstream method to obtain the information returned by monitoring the output stream of the process. String errormsg = readinputstream (PC. geterrorstream (), "error"); string outputmsg = readinputstream (PC. getinputstream (), "out"); int c = pc. waitfor (); If (C! = 0) {// If the processing process is waiting for system. out. println ("processing failed:" + errormsg);} else {system. out. println (this. complete + outputmsg);} catch (exception e) {system. out. println ("exception occurred during conversion processing") ;}/ ** create the command to convert to FLV */public list <string> toflv () {list <string> commend = new arraylist <string> (); // used for video conversion to FLV parameter setting // set the ffmpeg.exe path to commend. add (this. ffmpeg_path + "ffmpeg.exe"); commend. add ("-I"); // sets the commen of the file to be input. D. add (this. in_path); // location of the file to be input commend. add ("-y"); // overwrite the commend file of the same name in the output directory. add ("-acodec"); // sets the Audio Encoding mode commend. add ("libmp 3lame"); // The encoding method is MP3, which can be libmp 3lame or AAC commend. add ("-AR"); // sets the audio sampling frequency commend. add ("22050"); // The audio sampling rate can be set to 22050, 24000, 44100, or 48000 commend. add ("-qscale"); // sets the Dynamic Bit Rate commend. add ("6"); // The test shows that if you want to get high-quality videos, the smaller the value, the better commend. add (this. out_path); // set the output FLV file path return commend;}/** video creation command */Public list <string> getimage () {list <string> commend = new arraylist <string> (); // set commend for video parameters. add (this. ffmpeg_path + "FFMPEG"); // sets the location of the converter to commend. add ("-I"); // sets the commend of the file to be input. add (this. out_path); // location of the file to be input commend. add ("-y"); // overwrite the commend file of the same name in the output directory. add ("-F"); // output file format commend. add ("image2"); // JPG format commend. add ("-SS"); // you can switch the task commend from a specified time point. add ("8"); // commend from the video's 8th seconds. add ("-T "); // Set the record time commend. add ("1"); // The record time is 1 second commend. add ("-s"); // output image resolution commend. add ("200x200"); // picture size commend. add (this. out_path.substring (0, this. out_path.lastindexof (". ") + ". jpg "); // set the output file directory return commend;}/** the percentage of progress returned by monitoring the output stream of the process */private string readinputstream (inputstream is, string f) throws ioexception {// encapsulate the output stream of the process into a buffer reader object bufferedreader BR = new bufferedreader (New inputstreamread Er (is); stringbuffer lines = new stringbuffer (); // construct a variable string long totaltime = 0; // loop each row of the buffer reader object for (string line = BR. readline (); line! = NULL; line = BR. readline () {lines. append (line); // Add each line of Information string to the variable string int positionduration = line. indexof ("Duration:"); // locate int positiontime = line for the first "Duration:" in the current row. indexof ("time ="); If (positionduration> 0) {// if the current row contains "Duration:" string dur = line. replace ("Duration:", ""); // Replace "Duration:" in the current row with "" dur = dur. trim (). substring (0, 8); // removes the spaces at the beginning and end of the replaced string, and truncates the first eight characters, int H = integer. parseint (dur. substring (0, 2); // encapsulated into an hour int M = integer. parseint (dur. substring (3, 5); // encapsulated into minutes int S = integer. parseint (dur. substring (6, 8); // encapsulated into seconds totaltime = H * 3600 + M * 60 + S; // obtain the total time in seconds} If (positiontime> 0) {// if the time string used exists // extract the current time string containing time = string time = line. substring (positiontime, line. indexof ("bitrate")-1); time = time. substring (time. indexof ("=") + 1, time. indexof (". "); // capture the current time string float t = (float) Long. parselong (time)/(float) totaltime; // calculate the ratio of the time used to the total time required this. complete = (INT) math. ceil (T * 100); // percentage of computing completion progress} system. out. println ("Finished:" + this. complete + "%");} BR. close (); // close the output stream of the Process return lines. tostring ();}}
 
 
Post: http://blog.csdn.net/zdyfzj/article/details/6050541

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.