Java uses ffmpeg and mencoder for video format conversion

Source: Internet
Author: User

Starter: Personal blogs, continuous updates and corrections

Main Use technology:
1) FFmpeg, used for conversion between mainstream formats, such as avi,mp4,flv.
2) MEncoder, for the exotic format to mainstream format, such as RMVB to AVI. So we can put the wonderful format of the first AVI, and then by ffmpeg the AVI into the desired format.
3) Java executes the command-line operation of the technology, so installed on the server ↑ The two converters can be called by Java.
Including the Processbuilder and runtime of the two tuning methods.
You can refer to this article.


FFmpeg's official website is here, and its documentation is here.
MEncoder's official website is here, where the documentation is here.

Main references: This article, used by FFmpeg and MEncoder, is also directly used in the compression package provided later in this article.
But as some netizens point out, the problem with the code in this article is that when the rmvb->avi->flv two-step conversion, you need to wait for the previous step to complete before you take the next step.
So change the code, as follows:

Package test;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import java.util.List;
Import Java.util.UUID;

public class Test {

public static final String basepath = "d:\\ftest\\";

public static void Main (string[] args) {
String fromFilePath1 = basepath + "From\\test.mp4";
Doconvert (fromFilePath1, "flv");

String fromFilePath2 = basepath + "FROM\\TEST.RMVB";
Doconvert (fromFilePath2, "flv");
}

/**
* Try to convert
*/
public static void Doconvert (String fromfilepath, String goaltype) {
if (!checkinput (Fromfilepath)) {
System.out.println ("file" + Fromfilepath + "does not exist");
}else{
if (Process (Fromfilepath, goaltype)) {
System.out.println ("successful Conversion");
}else{
System.out.println ("conversion failed");
}
}
}

/**
* To convert
* @return
*/
private static Boolean process (string Fromfilepath, String goaltype) {
int type = Checkcontenttype (Fromfilepath);
Boolean status = FALSE;
if (type = = 0) {
Status = GetResult (Fromfilepath, Goaltype);
} else if (type = = 1) {
String Avifilepath = Getpreresult (type, fromfilepath);
if (Avifilepath = = null) {
return false;
}
Status = GetResult (Avifilepath, Goaltype);
}
return status;
}

/**
* Determine the type of source video (main format or wonderful format)
*/
private static int Checkcontenttype (String fromfilepath) {
String type = fromfilepath.substring (Fromfilepath.lastindexof (".") + 1, fromfilepath.length ())
. toLowerCase ();
FFmpeg can parse the format: (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv, etc.)
if (Type.equals ("avi")) {
return 0;
} else if (Type.equals ("mpg")) {
return 0;
} else if (Type.equals ("wmv")) {
return 0;
} else if (Type.equals ("3gp")) {
return 0;
} else if (Type.equals ("mov")) {
return 0;
} else if (Type.equals ("mp4")) {
return 0;
} else if (Type.equals ("ASF")) {
return 0;
} else if (Type.equals ("ASX")) {
return 0;
} else if (Type.equals ("flv")) {
return 0;
}
For ffmpeg unresolved file formats (WMV9,RM,RMVB, etc.),
You can use other tools (MEncoder) first to convert to AVI (ffmpeg) format.
else if (type.equals ("WMV9")) {
return 1;
} else if (Type.equals ("rm")) {
return 1;
} else if (Type.equals ("RMVB")) {
return 1;
}
return 9;
}

/**
* Check if the specified input file exists
*/
private static Boolean checkinput (String path) {
File File = new file (path);
if (!file.isfile ()) {
return false;
}
return true;
}

/**
* FFmpeg cannot parse the file format (WMV9,RM,RMVB, etc.), you can first use another tool (MEncoder) to convert to AVI (FFmpeg can parse) format.
*/
private static string Getpreresult (int type, string fromfilepath) {
String fileName = Uuid.randomuuid (). toString () + ". avi";

preprocessing directives
List<string> commend = new arraylist<string> ();
Commend.add (BasePath + "Util\\mencoder");
Commend.add (Fromfilepath);
Commend.add ("-oac lavc");
Commend.add ("-oac");
Commend.add ("Mp3lame");
Commend.add ("-lameopts");
Commend.add ("preset=64");
Commend.add ("-lavcopts");
Commend.add ("acodec=mp3:abitrate=64");
Commend.add ("-OVC");
Commend.add ("XviD");
Commend.add ("-xvidencopts");
Commend.add ("bitrate=600");
Commend.add ("-of");
Commend.add ("Avi");
Commend.add ("-O");
Commend.add (BasePath + "pre\\" + fileName);

try {
Preprocessing process
Processbuilder builder = new Processbuilder ();
Builder.command (commend);
Builder.redirecterrorstream (TRUE);

Process information output to the console
Process p = Builder.start ();
BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream ()));
String line = null;
while (line = Br.readline ()) = null) {
System.out.println (line);
}
P.waitfor ()///until the above command is executed.

return basepath + "pre\\" + fileName;
} catch (Exception e) {
E.printstacktrace ();
return null;
}
}

/**
* FFmpeg can parse the format: (asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv, etc.)
* @param Oldfilepath
* @param goaltype
* @return
*/
private static Boolean GetResult (String Oldfilepath, String goaltype) {
String fileName = uuid.randomuuid () + "." + Goaltype;

if (!checkinput (Oldfilepath)) {
System.out.println (Oldfilepath + "does not exist");
return false;
}

File naming
Calendar C = calendar.getinstance ();

Convert Format command
List<string> commend = new arraylist<string> ();
Commend.add (BasePath + "util\\ffmpeg");
Commend.add ("-i");
Commend.add (Oldfilepath);
Commend.add ("-ab");
Commend.add ("56");
Commend.add ("-ar");
Commend.add ("22050");
Commend.add ("-qscale");
Commend.add ("8");
Commend.add ("-R");
Commend.add ("15");
Commend.add ("-y");
Commend.add ("-S");
Commend.add ("600x500");
Commend.add (BasePath + "to\\" + fileName);

try {
Runtime runtime = Runtime.getruntime ();
Command
String cut = BasePath + "Util\\ffmpeg.exe-i"
+ Oldfilepath
+ "-y-f image2-ss 8-t 0.001-s 600x500" + basepath + "\\to\\" + fileName + ". jpg";

Process
Process proce = runtime.exec (cut);
Proce.waitfor ()///until the above command is executed.

Convert format process
Processbuilder builder = new Processbuilder (commend);
Builder.command (commend);
Builder.redirecterrorstream (TRUE);

Process information output to the console
Process p = Builder.start ();
BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream ()));
String line = null;
while (line = Br.readline ()) = null) {
System.out.println (line);
}
P.waitfor ()///until the above command is executed.

return true;
} catch (Exception e) {
E.printstacktrace ();
return false;
}
}
}

Long-term welcome project Cooperation Opportunity Introduction, project income 10% to reward introducer. Sina Weibo: @ Cold Mirror, qq:908789432.


Java uses ffmpeg and mencoder for video format conversion

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.