参考了一些资料,学习过程中尝试写一个转码工具,由于一些个人能力和其他的一些原因暂时搁置,特此记录一些代码,留作参考。
一、GUI:
package cn.com.huangyl.tool;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class FFmpegGUI {
private JFrame frame = new JFrame("FFmpegGUI");
private JTabbedPane tabbedPane = new JTabbedPane();
private JPanel panel1 = new JPanel(new BorderLayout());
private JPanel panelx = new JPanel(new BorderLayout());
private JPanel panely = new JPanel(new BorderLayout());
private JPanel panel2 = new JPanel(new BorderLayout());
private JPanel panel3 = new JPanel(new GridLayout(2, 1));
private JPanel panel4 = new JPanel(new BorderLayout());
private JPanel panel5 = new JPanel(new BorderLayout());
private JPanel panel6 = new JPanel(new BorderLayout());
private JPanel panel7 = new JPanel(new BorderLayout());
private JPanel panel8 = new JPanel(new BorderLayout());
private JPanel panel9 = new JPanel(new BorderLayout());
private JPanel panel10 = new JPanel(new BorderLayout());
private JPanel panel11 = new JPanel(new BorderLayout());
private JPanel panel12 = new JPanel(new GridLayout(6,1));
private JPanel panel13 = new JPanel(new GridLayout(6,1));
private JPanel panel14 = new JPanel(new BorderLayout());
private JPanel panel15 = new JPanel(new BorderLayout());
private JPanel panel16 = new JPanel(new BorderLayout());
private JPanel panel17 = new JPanel(new BorderLayout());
private JPanel panel18 = new JPanel(new GridLayout(1,2));
private JPanel panel19 = new JPanel(new BorderLayout());
private JPanel panel20 = new JPanel(new BorderLayout());
private JButton Select = new JButton("选择");
private JButton StartTranscode = new JButton("开始");
private JButton Reset = new JButton("重置");
//private JTextField display = new JTextField("请点击查询按钮");
private JTextField FilePath = new JTextField("请选择文件路径");
private JTextField VideoBitrate = new JTextField();
private JTextField resolution = new JTextField();
private JTextField AudioBitrate = new JTextField();
private JTextField AudioSampleRate = new JTextField();
private JTextField CreateFileName = new JTextField();
private JTextField CreateFileFormat = new JTextField();
private JLabel label1 = new JLabel("请选择文件");
private JLabel label2 = new JLabel("模板转码");
private JLabel label3 = new JLabel("请选择模板:");
private JLabel label4 = new JLabel("自由转码");
private JLabel label5 = new JLabel("视频码率:");
private JLabel label6 = new JLabel("分辨率:");
private JLabel label7 = new JLabel("音频码率:");
private JLabel label8 = new JLabel("音频采样率:");
private JLabel label9 = new JLabel("请选择转码方式:");
private JLabel label10 = new JLabel("留做进度条:");
private JLabel label11 = new JLabel("生成文件名称:");
private JLabel label12 = new JLabel("生成文件格式:");
private JComboBox Template = new JComboBox();
private JComboBox TranscodeWay = new JComboBox();
private JTextArea Log = new JTextArea("");
private JScrollPane scrollPane1 = new JScrollPane();
private JFileChooser stf = new JFileChooser(new File("d://"));
FFmpegGUI(){
frame.setLayout(null);
frame.setBounds(400, 200, 700, 420);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
tabbedPane.addTab("ffmpeg", this.panel1);
tabbedPane.addTab("ffplay", this.panelx);
tabbedPane.addTab("ffprobe", this.panely);
tabbedPane.setBounds(10, 10, 660, 360);
frame.add(this.tabbedPane);
panel1.add("West",panel2);
panel1.add("Center",panel19);
panel19.add("Center",Log);
panel19.add("South",label10);
panel2.add("North",panel3);
panel2.add("Center",panel8);
panel3.add(panel4);
panel3.add(panel5);
panel4.add("North",label1);
panel4.add("South",panel6);
panel5.add("North",label2);
panel5.add("South",panel7);
panel6.add("Center",FilePath);
panel6.add("East",Select);
Template.setModel(new DefaultComboBoxModel(new String[] { "1080p_1920x1080_2000", "SHD_1280x720_1200", "HD_960x540_800", "SD_640x360_500", "LD_360x240_300","..." }));
Template.setEditable(false);
panel7.add("Center",Template);
panel7.add("West",label3);
panel8.add("North",label4);
panel8.add("Center",panel9);
panel9.add("North",panel10);
panel9.add("Center",panel11);
panel10.add("West",panel12);
panel10.add("Center",panel13);
panel12.add(label5);
panel12.add(label6);
panel12.add(label7);
panel12.add(label8);
panel12.add(label11);
panel12.add(label12);
panel13.add(VideoBitrate);
panel13.add(resolution);
panel13.add(AudioBitrate);
panel13.add(AudioSampleRate);
panel13.add(CreateFileName);
panel13.add(CreateFileFormat);
panel11.add("North",panel14);
panel11.add("Center",panel15);
TranscodeWay.setModel(new DefaultComboBoxModel(new String[] { "模板转码", "自由转码" }));
TranscodeWay.setEditable(false);
panel14.add("West",label9);
panel14.add("Center",TranscodeWay);
panel15.add("Center",panel16);
panel15.add("South",panel17);
panel17.add("East",panel18);
panel18.add(Reset);
panel18.add(StartTranscode);
//文件选择
this.Select.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
FFmpegGUI.this.stf.setFileSelectionMode(0);
int state = FFmpegGUI.this.stf.showOpenDialog(null);
if(state == 1){
return;
}
File f = FFmpegGUI.this.stf.getSelectedFile();
FFmpegGUI.this.FilePath.setText(f.getAbsolutePath());
}
});
this.StartTranscode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String transcodeway = FFmpegGUI.this.TranscodeWay.getSelectedItem().toString();
if ("模板转码".equals(transcodeway)) {
String sourcefile = FFmpegGUI.this.FilePath.getText();
String Template = FFmpegGUI.this.TranscodeWay.getSelectedItem().toString();
FFmpegGUI.this.Log.setText(sourcefile);
Transcode t = new Transcode();
}
if ("自由转码".equals(transcodeway)){
String sourcefile = FFmpegGUI.this.FilePath.getText();
String VideoBitrate = FFmpegGUI.this.VideoBitrate.getText();
String resolution = FFmpegGUI.this.resolution.getText();
String AudioBitrate = FFmpegGUI.this.AudioBitrate.getText();
String AudioSampleRate = FFmpegGUI.this.AudioSampleRate.getText();
String CreateFileName = FFmpegGUI.this.CreateFileName.getText();
String CreateFileFormat = FFmpegGUI.this.CreateFileFormat.getText();
Transcode t = new Transcode();
try {
t.processTranscodefree(sourcefile,VideoBitrate,resolution,AudioBitrate,AudioSampleRate,CreateFileName,CreateFileFormat);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
public static void main(String args[]){
new FFmpegGUI();
}
}
二、Transcode
package cn.com.huangyl.tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Transcode {
/*
private static int checkContentType(){
String type = sourcefile.substring(PATH.lastIndexOf(".") + 1, sourcefile.length())
.toLowerCase();
//ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
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;
}
//对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
else if (type.equals("wmv9")) {
return 1;
} else if (type.equals("rm")) {
return 1;
} else if (type.equals("rmvb")) {
return 1;
}
return 9;
}
// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
private static String processAVI(int type) {
List<String> commend = new ArrayList<String>();
commend.add("c:\\ffmpeg\\mencoder");
commend.add(sourcefile);
commend.add("-oac");
commend.add("lavc");
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("c:\\ffmpeg\\output\\a.avi");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return "c:\\ffmpeg\\output\\a.avi";
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void processTranscodetemp(String Template,String sourcefile) throws Exception {
String type = sourcefile.substring(sourcefile.lastIndexOf(".") + 1, sourcefile.length())
.toLowerCase();
String F1080p_1920x1080_2000 = "ffmpeg -i "+sourcefile+"-vcodec h264 -b:v 2000k -r 25 -s 1920x1080 -pix_fmt yuv420p -g 33 -acodec aac -b:a 128k -ar 44100 -ac 2 -strict -2 -f "+ type +" -y "+ "1080P_2M."+type;
String SHD_1280x720_1200 = "ffmpeg -i "+sourcefile+"-vcodec h264 -b:v 1200k -r 25 -s 1280x720 -pix_fmt yuv420p -g 33 -acodec aac -b:a 128k -ar 44100 -ac 2 -strict -2 -f "+ type +" -y "+ "720P_1200."+type;
String HD_960x540_800 = "ffmpeg -i "+sourcefile+"-vcodec h264 -b:v 800k -r 25 -s 960x540 -pix_fmt yuv420p -g 33 -acodec aac -b:a 128k -ar 44100 -ac 2 -strict -2 -f "+ type +" -y "+ "540P_800."+type;
String SD_640x360_500 = "ffmpeg -i "+sourcefile+"-vcodec h264 -b:v 500k -r 25 -s 640x360 -pix_fmt yuv420p -g 33 -acodec aac -b:a 128k -ar 44100 -ac 2 -strict -2 -f "+ type +" -y "+ "360P_500."+type;
String LD_360x240_300 = "ffmpeg -i "+sourcefile+"-vcodec h264 -b:v 300k -r 25 -s 360x240 -pix_fmt yuv420p -g 33 -acodec aac -b:a 128k -ar 44100 -ac 2 -strict -2 -f "+ type +" -y "+ "240P_500."+type;
if (!checkfile(sourcefile)) {
System.out.println(sourcefile + " is not file");
}
try{
Runtime rt = Runtime.getRuntime();
Process proc;
proc = rt.exec(F1080p_1920x1080_2000);
// 取得命令结果的输出流
InputStream fis = proc.getInputStream();
// 用一个读输出流类去读
InputStreamReader isr = new InputStreamReader(fis);
// 用缓冲器读行
BufferedReader br = new BufferedReader(isr);
String line = null;
// 直到读完为止
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
*/
public boolean processTranscodefree(String sourcefile,String VideoBitrate,String resolution,String AudioBitrate,String AudioSampleRate,String CreateFileName,String CreateFileFormat) throws Exception{
File diretory = new File("");
String currPath = diretory.getAbsolutePath();
String outputPath = currPath + "\\output\\";
String ffmpegPath = currPath + "\\FFmpeg\\bin\\";
if (!checkfile(sourcefile)) {
System.out.println(sourcefile + " is not file");
}
// 文件命名
//Calendar c = Calendar.getInstance();
List<String> commend = new ArrayList<String>();
commend.add(ffmpegPath+"ffmpeg.exe");
//commend.add("F:\\java\\TranscodeGUI\\ffmpeg.exe");
commend.add("-i");
commend.add(sourcefile);
commend.add("-ab");
commend.add("-vcodec");
commend.add("h264");
//commend.add("");
commend.add("-b:v");
commend.add(VideoBitrate);
commend.add("-r");
commend.add("25");
commend.add("-s");
commend.add("resolution");
commend.add("-pix_fmt");
commend.add("yuv420p");
commend.add("-g");
commend.add("33");
commend.add("-acodec");
commend.add("aac");
commend.add("-b:a");
commend.add(AudioBitrate);
commend.add("-ac");
commend.add("2");
commend.add("-strict");
commend.add("-2");
commend.add("-f");
commend.add(CreateFileFormat);
commend.add("-y");
commend.add(outputPath+CreateFileName+"."+CreateFileFormat);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process proc = builder.start();
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
System.out.println(line);
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
return false;
}
return true;
}
/*
try {
Runtime rt = Runtime.getRuntime();
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
builder.start();
System.out.println("开始转码,请等待······");
} catch (Exception e) {
e.printStackTrace();
System.out.println("转码异常,请查看日志!");
}
*/
//检查文件是否存在
private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
}