jave
直接上代码:
package com.roots.cloudserver.util;
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.Multimediainfo;
import it.sauronsoftware.jave.VideoAttributes;
import it.sauronsoftware.jave.VideoSize;
import java.io.File;
/**
* 使用jave1.0.2.jar进行转码截帧
* @author chengjs
*
*/
public class MediaInfo {
public static boolean flag = true;
/**
* 视频转码
* 因转码出来不是H264的视频编码所以此方法弃用
* @param source
* @param targetPath
* @return
*/
public static boolean transcodingToMP4(File source,String targetPath){
//File source = new File("C:/Users/AdMinistrator/Downloads/厨房里的爆炸案.mpg");
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();// 音频属性
audio.setCodec("libmp3lame");// libmp3lame 音频编码
audio.setBitRate(new integer(128000));// 音频比特率
audio.setChannels(new Integer(1));// 声道
audio.setSamplingRate(new Integer(44100));// 采样率
VideoAttributes video = new VideoAttributes();// 视频属性
video.setCodec("libxvid");// 视频编码
video.setBitRate(new Integer(2048000));// 视频比特率
video.setFrameRate(new Integer(18));// 帧率 1f/s帧频,1是目前测试比较清楚的,越大越模糊
//video.setSize(new VideoSize(1920,1080));// 视频宽高
EncodingAttributes attrs = new EncodingAttributes();// 转码属性
attrs.setFormat("mp4");// 转码格式
attrs.setAudioAttributes(audio);// 音频属性
attrs.setVideoAttributes(video);// 视频属性
Encoder encoder = new Encoder();// 创建解码器
long beginTime = system.currenttimemillis();
try {
// 获取时长
MultimediaInfo m = encoder.getInfo(source);
System.out.println(m.getDuration()/1000 + "秒");
System.out.println("获取时长花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
beginTime = System.currentTimeMillis();
encoder.encode(source, target, attrs);
System.out.println("视频转码花费时间是:" + ((System.currentTimeMillis() - beginTime)/1000) + "秒");
flag = true;
} catch (illegalargumentException e) {
flag = false;
e.printstacktrace();
} catch (InputFormatException e) {
flag = false;
e.printStackTrace();
} catch (EncoderException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
/**
* 音频转码 转成MP3格式
* @param source
* @param targetPath
* @return
*/
public static boolean transcodingToMP3(File source,String targetPath){
//File source = new File("C:/Users/Administrator/Downloads/厨房里的爆炸案.mpg");
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();// 音频属性
audio.setCodec("libmp3lame");// libmp3lame 音频编码
audio.setBitRate(new Integer(128000));// 音频比特率
audio.setChannels(new Integer(1));// 声道
audio.setSamplingRate(new Integer(44100));// 采样率
EncodingAttributes attrs = new EncodingAttributes();// 视频属性
attrs.setFormat("mp3");// 转码格式
attrs.setAudioAttributes(audio);// 音频属性
Encoder encoder = new Encoder();// 创建解码器
long beginTime = System.currentTimeMillis();
try {
// 获取时长
MultimediaInfo m = encoder.getInfo(source);
System.out.println(m.getDuration()/1000 + "秒");
System.out.println("获取时长花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
beginTime = System.currentTimeMillis();
encoder.encode(source, target, attrs);
System.out.println("音频转码花费时间是:" + ((System.currentTimeMillis() - beginTime)/1000) + "秒");
flag = true;
} catch (IllegalArgumentException e) {
flag = false;
e.printStackTrace();
} catch (InputFormatException e) {
flag = false;
e.printStackTrace();
} catch (EncoderException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
/**
* 截取第一帧作为缩略图
* @param source
* @param targetPath
* @return
*/
public static boolean interceptionToJPG(File source,String targetPath){
//File source = new File("C:/Users/Administrator/Downloads/火箭少女101 - 卡路里.mp4");
File target = new File(targetPath);// 转图片
VideoAttributes video = new VideoAttributes();// 视频属性
video.setCodec("mjpeg");// 图片编码
video.setSize(new VideoSize(1200, 800));// 设置图片宽高
EncodingAttributes attrs = new EncodingAttributes();// 转码属性
attrs.setFormat("image2");// 转码格式
attrs.setoffset(3f);// 设置偏移位置,即开始转码位置(3秒)
attrs.setDuration(0.01f);// 设置转码持续时间(1秒)
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
long beginTime = System.currentTimeMillis();
try {
//获取时长
MultimediaInfo m = encoder.getInfo(source);
System.out.println(m.getDuration());
System.out.println("获取时长花费时间是:" + (System.currentTimeMillis() - beginTime));
beginTime = System.currentTimeMillis();
encoder.encode(source, target, attrs);
System.out.println("图片转码花费时间是:" + (System.currentTimeMillis() - beginTime));
flag = true;
} catch (IllegalArgumentException e) {
flag = false;
e.printStackTrace();
} catch (InputFormatException e) {
flag = false;
e.printStackTrace();
} catch (EncoderException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
public static Long getTime(File file) throws InputFormatException, EncoderException{
Encoder encoder = new Encoder();
MultimediaInfo m = encoder.getInfo(file);
long lengthOfTime = m.getDuration()/1000;
return lengthOfTime;
}
public static void main(String[] args) {
String fileOut = "E://30.mp4";
File file2 = new File(fileOut);
boolean flag1 = transcodingToMP3(file2,"C://别人家的小孩 .mp3");
boolean flag2 = interceptionToJPG(file2,"C://别人家的小孩 .jpg");
}
}
相关阅读
在servlet 中使用 request.getSession().getServletCo
今天恰好有个上传文件的练习需要使用 request.getSession().getServletContext().getRealPath("upload/" ); 获取上传到服务器上
Excel中的自动筛选具体该如何使用呢?下面是seo实验室小编带来的关于excel2007自动筛选的使用教程,希望阅读过后对你有所启发!excel
Excel中的绝对值函数具体该如何使用呢?下面是由seo实验室小编分享的excel 取绝对值函数的使用方法,以供大家阅读和学习。excel 取
示例图:activity.xml文件布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android
//strcpy函数的使用 #include<iostream> using namespace std; int main(){ char a[7] = "abcdef"; //不能写成char a[6] = "ab