From 262e472f0eb4904998f54a9f99d12d47f5837689 Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Sun, 20 Sep 2020 14:25:47 +0800 Subject: [PATCH] convert gif with palette convert gif with palette --- .../ffmpeg/activity/VideoHandleActivity.java | 15 +++++--- .../com/frank/ffmpeg/util/FFmpegUtil.java | 35 ++++++++++++++----- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java index ef8be14..3fde110 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java +++ b/app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java @@ -223,11 +223,18 @@ public class VideoHandleActivity extends BaseActivity { case R.id.btn_generate_gif://convert video into gif String Video2Gif = PATH + File.separator + "Video2Gif.gif"; int gifStart = 30; - int gifDuration = 5; - String resolution = "720x1280";//240x320、480x640、1080x1920 + int gifDuration = 10; + int width = 320; int frameRate = 10; - commandLine = FFmpegUtil.generateGif(srcFile, gifStart, gifDuration, - resolution, frameRate, Video2Gif); + String palettePath = PATH + "/palette.png"; + FileUtil.deleteFile(palettePath); + String[] paletteCmd = FFmpegUtil.generatePalette(srcFile, frameRate, width, palettePath); + String[] gifCmd = FFmpegUtil.generateGifByPalette(srcFile, palettePath, gifStart, gifDuration, + frameRate, width, Video2Gif); + List cmdList = new ArrayList<>(); + cmdList.add(paletteCmd); + cmdList.add(gifCmd); + ffmpegHandler.executeFFmpegCmds(cmdList); break; case R.id.btn_screen_record://screen recording // String screenRecord = PATH + File.separator + "screenRecord.mp4"; diff --git a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java index f53ea98..f36d3c0 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java +++ b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java @@ -284,22 +284,39 @@ public class FFmpegUtil { } /** - * convert video into gif + * generate a palette for gif * * @param srcFile input file + * @param frameRate frameRate of the gif + * @param width width + * @param targetFile output file + * @return generate palette success or not + */ + public static String[] generatePalette(String srcFile, int frameRate, int width, String targetFile) { + String paletteCmd = "ffmpeg -i %s -vf fps=%d,scale=%d:-1:flags=lanczos,palettegen %s"; + paletteCmd = String.format(Locale.getDefault(), paletteCmd, srcFile, frameRate, width, targetFile); + return paletteCmd.split(" "); + } + + /** + * convert video into gif with palette + * + * @param srcFile input file + * @param palette the palette which will apply to gif * @param startTime startTime in the video * @param duration duration, how long you want to - * @param targetFile output file - * @param resolution resolution of the gif * @param frameRate frameRate of the gif + * @param width width + * @param targetFile output gif * @return convert gif success or not */ - public static String[] generateGif(String srcFile, int startTime, int duration, - String resolution, int frameRate, String targetFile) { - String generateGifCmd = "ffmpeg -i %s -ss %d -t %d -s %s -r %d -f gif %s"; - generateGifCmd = String.format(Locale.getDefault(), generateGifCmd, srcFile, startTime, duration, - resolution, frameRate, targetFile); - return generateGifCmd.split(" "); + public static String[] generateGifByPalette(String srcFile, String palette, int startTime, int duration, + int frameRate, int width, String targetFile) { + String paletteGifCmd = "ffmpeg -i %s -i %s -ss %d -t %d -lavfi fps=%d,scale=%d:-1:flags=lanczos[x];[x][1:v]" + + "paletteuse=dither=bayer:bayer_scale=3 %s"; + paletteGifCmd = String.format(Locale.getDefault(), paletteGifCmd, srcFile, palette, startTime, + duration, frameRate, width, targetFile); + return paletteGifCmd.split(" "); } /**