java在线视频统计时间进度_Java 实现视频转FLV,支持完成进度百分比

本文介绍如何利用ffmpeg工具实现多种视频格式之间的转换,并详细解释了转换过程中的关键参数设置,包括音频比特率、采样率及视频帧率等。

视频转换主要采用ffmpeg,下载去官网即可

转换主程序:

注:以下标粗的 ffmpeg 是 绝对路径如:D:/conver/ffmpeg-win.exe

/**

*

* @Title: processFLV

* @Description: 转FLV格式

* ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)

* @param

* @return boolean

* @throws

*/

private static boolean coverToFLV(String srcVideoPath,String tarVideoPath) {

if (!checkfile(srcVideoPath)) {

logger.error("【" + srcVideoPath + "】 不存在 !");

return false;

}

List commend = new java.util.ArrayList();

commend.add(ffmpegPath);

commend.add( "-y");

commend.add( "-i");

commend.add(srcVideoPath);

commend.add("-ab");

commend.add("64");

//commend.add("-sameq");

commend.add("-acodec");

commend.add("mp3");

// commend.add("-vcodec");

// commend.add("xvid");

commend.add("-ac");

commend.add("2");

commend.add("-ar");

commend.add("22050");

commend.add("-qscale");

commend.add("6"); //压缩大小

// commend.add("-b");

// commend.add("1500");

commend.add("-r");

commend.add("24");

commend.add(tarVideoPath);

try {

ProcessBuilder builder = new ProcessBuilder();

//String cmd = commend.toString();

builder.command(commend);

Process p = builder.start();

doWaitPro(p);

//doWaitFor(p);

p.destroy();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

等待线程完成:

//等待线程处理完成

public static void doWaitPro(Process p){

try {

String errorMsg = readInputStream(p.getErrorStream(), "error");

String outputMsg = readInputStream(p.getInputStream(), "out");

int c = p.waitFor();

if (c != 0) {// 如果处理进程在等待

System.out.println("处理失败:" + errorMsg);

} else {

System.out.println(COMPLETE + outputMsg);

}

} catch (IOException e) {

// tanghui Auto-generated catch block

e.printStackTrace();

} catch (InterruptedException e) {

// tanghui Auto-generated catch block

e.printStackTrace();

}

}

完成进度百分比:

/**

*

* @Title: readInputStream

* @Description: 完成进度百分比

* @param

* @return String

* @throws

*/

private static String readInputStream(InputStream is, String f) throws IOException {

// 将进程的输出流封装成缓冲读者对象

BufferedReader br = new BufferedReader(new InputStreamReader(is));

StringBuffer lines = new StringBuffer();// 构造一个可变字符串

long totalTime = 0;

// 对缓冲读者对象进行每行循环

for (String line = br.readLine(); line != null; line = br.readLine()) {

lines.append(line);// 将每行信息字符串添加到可变字符串中

int positionDuration = line.indexOf("Duration:");// 在当前行中找到第一个"Duration:"的位置

int positionTime = line.indexOf("time=");

if (positionDuration > 0) {// 如果当前行中有"Duration:"

String dur = line.replace("Duration:", "");// 将当前行中"Duration:"替换为""

dur = dur.trim().substring(0, 8);// 将替换后的字符串去掉首尾空格后截取前8个字符

int h = Integer.parseInt(dur.substring(0, 2));// 封装成小时

int m = Integer.parseInt(dur.substring(3, 5));// 封装成分钟

int s = Integer.parseInt(dur.substring(6, 8));// 封装成秒

totalTime = h * 3600 + m * 60 + s;// 得到总共的时间秒数

}

if (positionTime > 0) {// 如果所用时间字符串存在

// 截取包含time=的当前所用时间字符串

String time = line.substring(positionTime, line

.indexOf("bitrate") - 1);

time = time.substring(time.indexOf("=") + 1, time.indexOf("."));// 截取当前所用时间字符串

int h = Integer.parseInt(time.substring(0, 2));// 封装成小时

int m = Integer.parseInt(time.substring(3, 5));// 封装成分钟

int s = Integer.parseInt(time.substring(6, 8));// 封装成秒

long hasTime = h * 3600 + m * 60 + s;// 得到总共的时间秒数

float t = (float) hasTime / (float) totalTime;// 计算所用时间与总共需要时间的比例

COMPLETE = (int) Math.ceil(t * 100);// 计算完成进度百分比

}

System.out.println("完成:" + COMPLETE + "%");

}

br.close();// 关闭进程的输出流

return lines.toString();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值