怎么在java中使用ffmpeg处理视频

这篇文章将为大家详细讲解有关怎么在java中使用ffmpeg处理视频,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。

1.下载并解压windows版本安装包

2.windows本地使用命令行测试

1.修改格式测试(转码)

  1. 将需要修改的视频A.avi 提前放在bin目录下

  2. 在bin目录下cmd进入命令行

  3. 输入命令完成转码成B.mp4

ffmpeg.exe -i A.avi -y B.mp4

2.视频音频结合测试

  1. 将需要修改的视频A.avi和bgm.mp3 提前放在bin目录下

  2. 在bin目录下cmd进入命令行

  3. 输入命令完成合并成8秒的new.avi

ffmpeg.exe -i A.avi -i bgm.mp3 -t 8 -y new.avi

3.java中建立工具测试类

packagecom.xc.utils;
importjava.io.BufferedReader;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.List;
publicclassFFMpegTest{
	privateStringffmpegEXE;
	
	publicFFMpegTest(StringffmpegEXE){
		super();
		this.ffmpegEXE=ffmpegEXE;
	}
	
	publicvoidconvertor(StringvideoInputPath,StringvideoOutputPath)throwsException{
//		ffmpeg-iinput.mp4-youtput.avi
		Listcommand=newArrayList<>();
		command.add(ffmpegEXE);
		
		command.add("-i");
		command.add(videoInputPath);
		command.add("-y");
		command.add(videoOutputPath);
		
		for(Stringc:command){
			System.out.print(c+"");
		}
		
		ProcessBuilderbuilder=newProcessBuilder(command);
		Processprocess=builder.start();
		
		InputStreamerrorStream=process.getErrorStream();
		InputStreamReaderinputStreamReader=newInputStreamReader(errorStream);
		BufferedReaderbr=newBufferedReader(inputStreamReader);
		
		Stringline="";
		while((line=br.readLine())!=null){
		}
		
		if(br!=null){
			br.close();
		}
		if(inputStreamReader!=null){
			inputStreamReader.close();
		}
		if(errorStream!=null){
			errorStream.close();
		}
		
	}
	publicstaticvoidmain(String[]args){
		FFMpegTestffmpeg=newFFMpegTest("C:\\ffmpeg\\bin\\ffmpeg.exe");
		try{
			ffmpeg.convertor("C:\\a.mp4","C:\\b.avi");
		}catch(Exceptione){
			e.printStackTrace();
		}
	}
}
packagecom.xc.utils;
importjava.io.BufferedReader;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.List;
publicclassMergeVideoMp3{
	privateStringffmpegEXE;
	
	publicMergeVideoMp3(StringffmpegEXE){
		super();
		this.ffmpegEXE=ffmpegEXE;
	}
	
	publicvoidconvertor(StringvideoInputPath,Stringmp3InputPath,
			doubleseconds,StringvideoOutputPath)throwsException{
//		ffmpeg.exe-iA.avi-ibgm.mp3-t7-ynew.avi
		Listcommand=newArrayList<>();
		command.add(ffmpegEXE);
		
		command.add("-i");
		command.add(videoInputPath);
		
		command.add("-i");
		command.add(mp3InputPath);
		
		command.add("-t");
		command.add(String.valueOf(seconds));
		
		command.add("-y");
		command.add(videoOutputPath);
		
//		for(Stringc:command){
//			System.out.print(c+"");
//		}
		
		ProcessBuilderbuilder=newProcessBuilder(command);
		Processprocess=builder.start();
		
		InputStreamerrorStream=process.getErrorStream();
		InputStreamReaderinputStreamReader=newInputStreamReader(errorStream);
		BufferedReaderbr=newBufferedReader(inputStreamReader);
		
		Stringline="";
		while((line=br.readLine())!=null){
		}
		
		if(br!=null){
			br.close();
		}
		if(inputStreamReader!=null){
			inputStreamReader.close();
		}
		if(errorStream!=null){
			errorStream.close();
		}
		
	}
	publicstaticvoidmain(String[]args){
		MergeVideoMp3ffmpeg=newMergeVideoMp3("C:\\ffmpeg\\bin\\ffmpeg.exe");
		try{
			ffmpeg.convertor("C:\\a.avi","C:\\bgm.mp3",7.1,"C:\\javaNew.mp4");
		}catch(Exceptione){
			e.printStackTrace();
		}
	}
}

关于怎么在java中使用ffmpeg处理视频就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

发布于 2021-03-21 22:39:33
分享
海报
163
上一篇:如何在Go语言中使用正则表达式下一篇:使用wxpy怎么自动发送微信消息
目录

    忘记密码?

    图形验证码