一、前言
最近写了一款图形界面版的音视频格式转化器,可以实现将多种视频之间进行转化,非常好用,如将AVI转换为,TS,FLV,MP4等。音频可将MP3转成WAV。
二、实现
1.需引入相关maven依赖。
<!-- 核心包 -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>3.4.0</version>
</dependency>
<!-- Windows64位 -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-win64</artifactId>
<version>3.4.0</version>
</dependency>
2.编写相关音视频格式转换代码,如下:
public static ConvertResult audioToAudioConverter(File source, File target, String audioCodec,String outFormat) {
ConvertResult result = new ConvertResult();
try {
result.setSuccess(true);
//设置音频流的编码属性
AudioAttributes audio = new AudioAttributes();
//libmp3lame表示使用LAME MP3编码器。LAME是一个开源的MP3编码器,FFmpeg中的libmp3lame就是LAME的实现。
if (Objects.nonNull(audioCodec) && !audioCodec.equals("")) {
audio.setCodec(audioCodec);
}
if (Objects.isNull(audioCodec) || Objects.equals(audioCodec,"libmp3lame")) {
//在MP3编码中,通常的比特率范围是64 kbps到320 kbps。推荐的范围是128 kbps到256 kbps。较低的比特率会导致音频质量下降,但文件大小更小。
audio.setBitRate(128000);
//1单声道,2立体声
audio.setChannels(2);
//设置音频的采样率为44100Hz。这是CD质量的音频的标准采样率。
audio.setSamplingRate(44100);
}else {
if (Objects.equals(audioCodec,"libmp3lame")) {
//设置音频的采样率为44100Hz。这是CD质量的音频的标准采样率。
audio.setSamplingRate(44100);
//8BITS,16BTIS,24BTIS,32BTIS
audio.setBitRate(1411100); //对于16bit
audio.setChannels(2);
}
}
EncodingAttributes attrs = new EncodingAttributes();
if (Objects.nonNull(outFormat) && !outFormat.equals("")) {
attrs.setOutputFormat(outFormat);
}
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
MultimediaObject multimediaObject = new MultimediaObject(source);
encoder.encode(multimediaObject, target, attrs);
}catch (Exception e) {
result.setSuccess(false);
result.setMessage(e.getMessage());
System.err.println(e);
}
return result;
}
public static ConvertResult converterToWav(File source,File target) {
//wav编码格式 pcm_s16le 指定输出音频编码为PCM 16位小端序,指定输出音频编码为PCM 16位大端序,s表示signed u表示unsigned
return audioToAudioConverter(source,target,"pcm_s16le","wav");
}
.............................
3.编写图形界面。
public class AudioVideoConverter {
private List<String> errorMsgList = new ArrayList<>();
static JLabel videoConvertJl,audioConvertJl,videoText,audioText;
static JPanel videoJPanel,audioJPanel;
boolean videoFlag = true;
boolean audioFlag = false;
static {
videoConvertJl = new JLabel(new ImageIcon(AudioVideoConverter.class.getResource("/image/videoConvert.png")));
audioConvertJl = new JLabel(new ImageIcon(AudioVideoConverter.class.getResource("/image/videoConvert.png")));
videoText = new JLabel("视频转换");
audioText = new JLabel("音频转换");
videoJPanel = new JPanel();
audioJPanel = new JPanel();
videoJPanel.setBounds(50,4,62,62);
videoJPanel.setBackground(new Color(55,50,50));
videoConvertJl.setBounds(50, 3, 60, 60);
videoJPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
videoJPanel.add(videoConvertJl);
videoText.setBounds(60, 71, 60, 15);
audioConvertJl.setBounds(160, 4, 60, 60);
audioJPanel.setBounds(160, 3, 62, 62);
audioJPanel.setBackground(null);
audioJPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
audioJPanel.add(audioConvertJl);
audioText.setBounds(170, 71, 80, 15);
}
...............................
public static void main( String[] args )
{
JFrame frame=new JFrame();
AudioVideoConverter converter=new AudioVideoConverter();
//视频转换与音频转换切换
JLabel jl=new JLabel("选择转换到的格式:");
String[] items = {"MP4", "MKV", "AVI", "WMV","TS","FLV","3GP"};
final JComboBox<String> comboBox = new JComboBox<>(items);
comboBox.setEditable(false);
comboBox.setBounds(170, 105, 60, 20);
final JComboBox<String> audioComboBox = converter.setAudioComboBox();
JButton b=new JButton("选择文件");
b.setHorizontalTextPosition(JButton.CENTER);
//b.setBackground(new Color(195,17,17));
b.setBorder(BorderFactory.createRaisedSoftBevelBorder());
//去掉按钮文字周围的焦点框
b.setFocusPainted(false);
b.setBounds(240, 105, 80, 20);
b.setVisible(true);
jl.setBounds(50, 100, 150, 30);
JTextArea area=new JTextArea(8,10);
area.setBounds(50, 140, 550, 170);
area.setBorder(BorderFactory.createLoweredSoftBevelBorder());
area.setLineWrap(true);
area.setVisible(true);
area.setText("原视频地址\n");
area.setEditable(false);
JLabel jlPosition=new JLabel("保存位置:");
jlPosition.setFont(new Font("宋体",Font.BOLD,14));
jlPosition.setBounds(50, 320, 100, 30);
ButtonGroup group = new ButtonGroup();
JRadioButton option1 = new JRadioButton("保存在原视频相同位置");
JRadioButton option2 = new JRadioButton("指定位置");
group.add(option1);
group.add(option2);
// 默认选中第一个选项
option1.setSelected(true);
option1.setBackground(null);
option2.setBackground(null);
option1.setBounds(50, 360, 200, 20);
option2.setBounds(50, 390, 80, 20);
JTextField jt = new JTextField("");
jt.setBounds(130, 390, 370, 20);
JButton selectButton=new JButton("选择文件夹");
selectButton.setHorizontalTextPosition(JButton.CENTER);
selectButton.setBorder(BorderFactory.createRaisedSoftBevelBorder());
//去掉按钮文字周围的焦点框
selectButton.setFocusPainted(false);
selectButton.setBounds(520, 390, 80, 20);
selectButton.setVisible(true);
selectButton.setEnabled(false);
//开始转换
JButton exchangeBt=new JButton("开始转换");
exchangeBt.setHorizontalTextPosition(JButton.CENTER);
exchangeBt.setBorder(BorderFactory.createRaisedSoftBevelBorder());
//去掉按钮文字周围的焦点框
exchangeBt.setFocusPainted(false);
exchangeBt.setBounds(260, 470, 80, 20);
exchangeBt.setVisible(true);
frame.setSize(680, 600);
frame.setTitle("音视频转换");
frame.setLocationRelativeTo(null);
// 创建一个ImageIcon对象
ImageIcon icon = new ImageIcon(AudioVideoConverter.class.getResource("/image/videoAudioIcon.png"));
// 调整图标大小
Image image = icon.getImage(); // 获取原始图像
Image newImage = image.getScaledInstance(16, 16, Image.SCALE_SMOOTH); // 设置新的大小,这里是16x16像素
icon.setImage(newImage); // 设置调整大小后的图像
frame.setIconImage(newImage);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Container c=frame.getContentPane();
c.setLayout(null);
c.add(videoJPanel);
c.add(videoText);
c.add(audioJPanel);
c.add(audioText);
c.add(area);
c.add(jl);
c.add(jlPosition);
c.add(comboBox);
c.add(audioComboBox);
c.add(option1);
c.add(option2);
c.add(jt);
c.add(b);
c.add(selectButton);
c.add(exchangeBt);
.............................
//converter.processLoadingAction(exchangeBt,frame,b,selectButton,fileList,comboBox,jt,option1,option2,audioComboBox);
c.setBackground(new Color(222,184,135));
}
接着启动main方法接口了。
效果如下:
完整代码如下:
java swing实现音视频转换器