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

Posted 雨后丶云初霁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现压缩mp4视频适应网页播放相关的知识,希望对你有一定的参考价值。

1,安装ffmpeg

 

2,java代码:摘自  https://blog.csdn.net/javaer_lee/article/details/79425201

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 = diretory.getAbsolutePath();
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<String> commend = new ArrayList<String>();
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 = builder.command(commend).redirectErrorStream(true).start();
new PrintStream(process.getInputStream());
new PrintStream(process.getErrorStream());
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");
return false;
}

List<String> command = new ArrayList<String>();
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.getRuntime().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 PrintStream(videoProcess.getErrorStream()).start();
//
// new PrintStream(videoProcess.getInputStream()).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
{
while(this != null)
{
int _ch = __is.read();
if(_ch != -1)
System.out.print((char)_ch);
else break;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}










































































































































































































以上是关于java实现压缩mp4视频适应网页播放的主要内容,如果未能解决你的问题,请参考以下文章

iOS h5点播播放mp4视频遇到的坑,ios的h5不能播放视频等

ATP应用测试平台——使用vue-video-player视频播放组件实现网页视频流的播放案例实战

网页播放摄像头视频 一种新的实现方式(非ocx方式)

FFMPEG+SDL实现视频播放器

linux系统中Nginx+FFmPeg+vlc实现网页视频播放配置过程

jPlayer 视频播放