accurate seek to start time

accurate seek to start time
pull/166/head
xufulong 4 years ago
parent 29f7706ad9
commit d7c961088f
  1. 12
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java

@ -42,7 +42,7 @@ public class FFmpegUtil {
* @return cut success or not * @return cut success or not
*/ */
public static String[] cutAudio(String inputPath, int startTime, int duration, String outputPath) { public static String[] cutAudio(String inputPath, int startTime, int duration, String outputPath) {
String cutAudioCmd = "ffmpeg -ss %d -t %d -i %s -acodec copy -vn %s"; String cutAudioCmd = "ffmpeg -ss %d -accurate_seek -t %d -i %s -acodec copy -vn %s";
cutAudioCmd = String.format(Locale.getDefault(), cutAudioCmd, startTime, duration, inputPath, outputPath); cutAudioCmd = String.format(Locale.getDefault(), cutAudioCmd, startTime, duration, inputPath, outputPath);
return cutAudioCmd.split(" "); return cutAudioCmd.split(" ");
} }
@ -200,7 +200,7 @@ public class FFmpegUtil {
* @return cut video success or not * @return cut video success or not
*/ */
public static String[] cutVideo(String inputPath, int startTime, int duration, String outputPath) { public static String[] cutVideo(String inputPath, int startTime, int duration, String outputPath) {
String cutVideoCmd = "ffmpeg -ss %d -t %d -i %s -acodec copy -vcodec copy %s"; String cutVideoCmd = "ffmpeg -ss %d -accurate_seek -t %d -i %s -acodec copy -vcodec copy -no_negative_ts 1 %s";
cutVideoCmd = String.format(Locale.getDefault(), cutVideoCmd, startTime, duration, inputPath, outputPath); cutVideoCmd = String.format(Locale.getDefault(), cutVideoCmd, startTime, duration, inputPath, outputPath);
return cutVideoCmd.split(" "); return cutVideoCmd.split(" ");
} }
@ -294,7 +294,7 @@ public class FFmpegUtil {
*/ */
public static String[] generatePalette(String inputPath, int startTime, int duration, public static String[] generatePalette(String inputPath, int startTime, int duration,
int frameRate, int width, String outputPath) { int frameRate, int width, String outputPath) {
String paletteCmd = "ffmpeg -ss %d -t %d -i %s -vf fps=%d,scale=%d:-1:flags=lanczos,palettegen %s"; String paletteCmd = "ffmpeg -ss %d -accurate_seek -t %d -i %s -vf fps=%d,scale=%d:-1:flags=lanczos,palettegen %s";
paletteCmd = String.format(Locale.getDefault(), paletteCmd, startTime, paletteCmd = String.format(Locale.getDefault(), paletteCmd, startTime,
duration, inputPath, frameRate, width, outputPath); duration, inputPath, frameRate, width, outputPath);
return paletteCmd.split(" "); return paletteCmd.split(" ");
@ -314,7 +314,7 @@ public class FFmpegUtil {
*/ */
public static String[] generateGifByPalette(String inputPath, String palette, int startTime, int duration, public static String[] generateGifByPalette(String inputPath, String palette, int startTime, int duration,
int frameRate, int width, String outputPath) { int frameRate, int width, String outputPath) {
String paletteGifCmd = "ffmpeg -ss %d -t %d -i %s -i %s -lavfi fps=%d,scale=%d:-1:flags=lanczos[x];[x][1:v]" + String paletteGifCmd = "ffmpeg -ss %d -accurate_seek -t %d -i %s -i %s -lavfi fps=%d,scale=%d:-1:flags=lanczos[x];[x][1:v]" +
"paletteuse=dither=bayer:bayer_scale=3 %s"; "paletteuse=dither=bayer:bayer_scale=3 %s";
paletteGifCmd = String.format(Locale.getDefault(), paletteGifCmd, startTime, paletteGifCmd = String.format(Locale.getDefault(), paletteGifCmd, startTime,
duration, inputPath, palette, frameRate, width, outputPath); duration, inputPath, palette, frameRate, width, outputPath);
@ -430,10 +430,10 @@ public class FFmpegUtil {
int frameRate, int width, String outputPath) { int frameRate, int width, String outputPath) {
String toImage; String toImage;
if (width > 0) { if (width > 0) {
toImage = "ffmpeg -ss %d -t %d -i %s -an -vf fps=%d,scale=%d:-1 %s"; toImage = "ffmpeg -ss %d -accurate_seek -t %d -i %s -an -vf fps=%d,scale=%d:-1 %s";
toImage = String.format(Locale.CHINESE, toImage, startTime, duration, inputPath, frameRate, width, outputPath); toImage = String.format(Locale.CHINESE, toImage, startTime, duration, inputPath, frameRate, width, outputPath);
} else { } else {
toImage = "ffmpeg -ss %d -t %d -i %s -an -r %d %s"; toImage = "ffmpeg -ss %d -accurate_seek -t %d -i %s -an -r %d %s";
toImage = String.format(Locale.CHINESE, toImage, startTime, duration, inputPath, frameRate, outputPath); toImage = String.format(Locale.CHINESE, toImage, startTime, duration, inputPath, frameRate, outputPath);
} }
toImage = toImage + "%3d.png"; toImage = toImage + "%3d.png";

Loading…
Cancel
Save