java能不能播放mp4_java实现压缩mp4视频适应网页播放

java能不能播放mp4_java实现压缩mp4视频适应⽹页播放1,安装ffmpeg
package test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class ConvertVideo {
private static String inputPath = "";
private static String outputPath = "";
private static String ffmpegPath = "";
public static void main(String args[]) throws IOException {
getPath();
if (!checkfile(inputPath)) {
System.out.println(inputPath + " is not file");
return;
}
if (process()) {
System.out.println("ok");
}
}
private static void getPath() { // 先获取当前项⽬路径,在获得源⽂件、⽬标⽂件、转换器的路径
File diretory = new File("");
try {
String currPath = AbsolutePath();
inputPath = "E:\\1.mp4";
outputPath = "E:\\6.mp4";
ffmpegPath = "C:\\Users\\chenwei\\Desktop\\ffmpeg-20190104-b9523f2-win64-static\\bin\\";
System.out.println(currPath);
}
catch (Exception e) {
System.out.println("getPath出错");
}
}
private static boolean process() {
int type = checkContentType();
boolean status = false;
if (type == 0) {
System.out.println("直接转成flv格式");
status = processFLV(inputPath);// 直接转成flv格式
} else if (type == 1) {
String avifilepath = processAVI(type);
if (avifilepath == null)
return false;// 没有得到avi格式
status = processFLV(avifilepath);// 将avi转成flv格式
}
return status;
}
private static int checkContentType() {
String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.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;
}
private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
/
/ 对ffmpeg⽆法解析的⽂件格式(wmv9,rm,rmvb等), 可以先⽤别的⼯具(mencoder)转换为avi(ffmpeg能解析的)格式. private static String processAVI(int type) {
List commend = new ArrayList();
commend.add(ffmpegPath + "mencoder");
commend.add(inputPath);
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=1000");
commend.add("-of");
commend.add("mp4");
commend.add("-o");
commend.add(outputPath + "a.mp4");
try {
ProcessBuilder builder = new ProcessBuilder();
Process process = buildermand(commend).redirectErrorStream(true).start(); new InputStream());
new ErrorStream());
process.waitFor();
return outputPath + "a.mp4";
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) private static boolean processFLV(String oldfilepath) {
if (!checkfile(inputPath)) {
System.out.println(oldfilepath + " is not file");
diypda
return false;
}
List command = new ArrayList();
command.add(ffmpegPath + "ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-ab");
command.add("56");
command.add("-ar");
command.add("22050");
command.add("-qscale");
垂直母排command.add("8");
command.add("-r");
command.add("15");
沟槽式管接头
command.add("-s");
command.add("600x500");
command.add(outputPath + "a.mp4");
try {
// ⽅案1
Process videoProcess = Runtime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath + " -vcodec libx264 -r 29.97 -b 768k -ar 24000 -ab 64k -s 1280x720 "
+ outputPath + "a.mp4");
// ⽅案2
自由草
// Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start(); //
// new ErrorStream()).start();
//
// new InputStream()).start();
//
// videoProcess.waitFor();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
class PrintStream extends Thread
{
java.io.InputStream __is = null;
public PrintStream(java.io.InputStream is)
{
__is = is;
}
存取一体机public void run()
{
try

本文发布于:2024-09-22 23:29:08,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/210326.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:格式   播放   路径   适应   视频   实现   解析
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议