convert gif with palette

convert gif with palette
pull/166/head
xufulong 4 years ago
parent cd299a36a3
commit 262e472f0e
  1. 15
      app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
  2. 35
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.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<String[]> 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";

@ -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(" ");
}
/**

Loading…
Cancel
Save