1.在linux下使用的话,需要导入jave包
//java代码
private boolean ffmpegChange1 (File file,File fileSave){
try{
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(56000));
audio.setChannels(new Integer(1));
audio.setSamplingRate(new Integer(22050));
VideoAttributes video = new VideoAttributes();
// video.setCodec(myConfig.getFileFFmpegPath());
// video.setCodec("libx264");
video.setCodec("mpeg4");
// video.setCodec("h264");
video.setBitRate(new Integer(800000));
video.setFrameRate(new Integer(15));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
long beginTime = System.currentTimeMillis();
MultimediaInfo m = encoder.getInfo(file);
System.out.println(m.getDuration()/1000 + "秒");
System.out.println("获取时长花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
beginTime = System.currentTimeMillis();
// encoder.encode(source, fileSave, attrs);
encoder.encode(file, fileSave, attrs);
// System.out.println("压缩完成...");
System.out.println("压缩完成花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
return true;
}catch (EncoderException e){
e.printStackTrace();
System.out.println("压缩错误信息是1:"+e.getMessage());
return false;
}
}
//pom文件引用
java
jave
1.0.2.
system
${project.basedir}/src/main/resources/lib/jave-1.0.2.jar
2.在Mac上跑
private boolean ffmpegChange (File file,File fileSave){
try{
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(56000));
audio.setChannels(new Integer(1));
audio.setSamplingRate(new Integer(22050));
VideoAttributes video = new VideoAttributes();
//video.setCodec("mpeg4");
video.setCodec("h264");
video.setBitRate(new Integer(800000));
video.setFrameRate(new Integer(1));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder(new MyFFMPEGExecute());
long beginTime = System.currentTimeMillis();
MultimediaInfo m = encoder.getInfo(file);
System.out.println(m.getDuration()/1000 + "秒");
System.out.println("获取时长花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
beginTime = System.currentTimeMillis();
// encoder.encode(source, fileSave, attrs);
encoder.encode(file, fileSave, attrs);
// System.out.println("压缩完成...");
System.out.println("压缩完成花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
return true;
}catch (EncoderException e){
e.printStackTrace();
System.out.println("压缩错误信息是:"+e.getMessage());
return false;
}
}
//重写ffmpeg地址,path我用的是绝对路径,获取ffmpeg
public class MyFFMPEGExecute extends FFMPEGLocator {
@Autowired
MyConfig myConfig;
@Override
protected String getFFMPEGExecutablePath() {
String path1 = "/Users/apple/Desktop/imagecollect/file/ffmpeg";
String path2 = "/root/imagecollect/ffmpeg";
// System.out.println("ffmpegways = " + path);
return path1;
}
}
//pom文件引用
java
jave
2.0
system
${project.basedir}/src/main/resources/lib/jave-2.0.jar
3.在linux中跑 用jave.2.0.jar的方法,代码同上,唯一区别是ffmpeg的执行包不同
//重写ffmpeg地址,path我用的是绝对路径,获取ffmpeg
public class MyFFMPEGExecute extends FFMPEGLocator {
@Autowired
MyConfig myConfig;
@Override
protected String getFFMPEGExecutablePath() {
String path1 = "/Users/apple/Desktop/imagecollect/file/ffmpeg";
String path2 = "/root/imagecollect/ffmpeg";
//此处用path2,此路径是服务器上绝对路径,记得把ffmpeg放到此路径位置就可以
return path2;
}
}