java使用ffmpeg处理视频的方法
FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。
官网链接http://ffmpeg.org/
1.下载并解压windows版本安装包
2.windows本地使用命令行测试
1.修改格式测试(转码)
将需要修改的视频A.avi 提前放在bin目录下 在bin目录下cmd进入命令行 输入命令完成转码成B.mp4ffmpeg.exe -i A.avi -y B.mp4
2.视频音频结合测试
将需要修改的视频A.avi和bgm.mp3 提前放在bin目录下 在bin目录下cmd进入命令行 输入命令完成合并成8秒的new.aviffmpeg.exe -i A.avi -i bgm.mp3 -t 8 -y new.avi
3.java中建立工具测试类
package com.xc.utils;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;public class FFMpegTest {private String ffmpegEXE;public FFMpegTest(String ffmpegEXE) {super();this.ffmpegEXE = ffmpegEXE;}public void convertor(String videoInputPath, String videoOutputPath) throws Exception {//ffmpeg -i input.mp4 -y output.aviList<String> command = new ArrayList<>();command.add(ffmpegEXE);command.add('-i');command.add(videoInputPath);command.add('-y');command.add(videoOutputPath);for (String c : command) {System.out.print(c + ' ');}ProcessBuilder builder = new ProcessBuilder(command);Process process = builder.start();InputStream errorStream = process.getErrorStream();InputStreamReader inputStreamReader = new InputStreamReader(errorStream);BufferedReader br = new BufferedReader(inputStreamReader);String line = '';while ( (line = br.readLine()) != null ) {}if (br != null) {br.close();}if (inputStreamReader != null) {inputStreamReader.close();}if (errorStream != null) {errorStream.close();}}public static void main(String[] args) {FFMpegTest ffmpeg = new FFMpegTest('C:ffmpegbinffmpeg.exe');try {ffmpeg.convertor('C:a.mp4', 'C:b.avi');} catch (Exception e) {e.printStackTrace();}}}
package com.xc.utils;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;public class MergeVideoMp3 {private String ffmpegEXE;public MergeVideoMp3(String ffmpegEXE) {super();this.ffmpegEXE = ffmpegEXE;}public void convertor(String videoInputPath, String mp3InputPath,double seconds, String videoOutputPath) throws Exception {//ffmpeg.exe -i A.avi -i bgm.mp3 -t 7 -y new.aviList<String> command = new ArrayList<>();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 (String c : command) {//System.out.print(c + ' ');//}ProcessBuilder builder = new ProcessBuilder(command);Process process = builder.start();InputStream errorStream = process.getErrorStream();InputStreamReader inputStreamReader = new InputStreamReader(errorStream);BufferedReader br = new BufferedReader(inputStreamReader);String line = '';while ( (line = br.readLine()) != null ) {}if (br != null) {br.close();}if (inputStreamReader != null) {inputStreamReader.close();}if (errorStream != null) {errorStream.close();}}public static void main(String[] args) {MergeVideoMp3 ffmpeg = new MergeVideoMp3('C:ffmpegbinffmpeg.exe');try {ffmpeg.convertor('C:a.avi', 'C:bgm.mp3', 7.1, 'C:javaNew.mp4');} catch (Exception e) {e.printStackTrace();}}}
总结
到此这篇关于java使用ffmpeg处理视频的方法的文章就介绍到这了,更多相关java ffmpeg处理视频内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章: