1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java利用FFMpeg将mp4转换为gif

java利用FFMpeg将mp4转换为gif

时间:2022-09-11 16:13:15

相关推荐

java利用FFMpeg将mp4转换为gif

网上和官方资料整合

存在问题,在不丢帧的情况下转换后的gif会比原始mp4文件大很多。

package jinx;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;/*** 利用ffmpeg将mp4转换为gif*/public class FFMpegUtil {//Windows下 ffmpeg.exe的路径//private static String ffmpegEXE = "D:\\Downloads\\ffmpeg-0528-ebf85d3-win64-static\\bin\\ffmpeg.exe";//Linux与mac下 ffmpeg的路径private static String ffmpegEXE = "/usr/local/Cellar/ffmpeg/4.1.2/bin/ffmpeg";/**** @param time 截取视频长度* @param start截取开始时间 如果为null表示全部转换为gif* @param inputPath 被转换的mp4文件位置* @param outPath 转换后gif文件位置* @throws Exception*/public static void convetor(int time,String start,String inputPath, String outPath) throws Exception {List<String> command = new ArrayList<String>();command.add(ffmpegEXE);if(0!=time){command.add("-t");command.add(String.valueOf(time));}if(start!=null&&!"00:00:00".equals(start)){command.add("-ss");command.add(start);}command.add("-i");command.add(inputPath);command.add(outPath);ProcessBuilder builder = new ProcessBuilder(command);Process process = null;try {process = builder.start();} catch (IOException e) {e.printStackTrace();}//使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理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) {String videoInputPath = "/Users/jinx/Downloads/1.mp4";String coverOutputPath = "/Users/jinx/Downloads/4.gif";try {convetor(5,"00:00:01",videoInputPath,coverOutputPath);} catch (Exception e) {e.printStackTrace();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。