Skip to content

Instantly share code, notes, and snippets.

@rutvik110
Created December 27, 2022 02:03
Show Gist options
  • Save rutvik110/319f065cfd5170a7cc255e42f86c29ff to your computer and use it in GitHub Desktop.
Save rutvik110/319f065cfd5170a7cc255e42f86c29ff to your computer and use it in GitHub Desktop.
FFmpegKit helper with imagesToVideo, imagesToGif, videoToGif helper
import 'dart:developer' as dev;
import 'dart:io';
import 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
class FFMPEGKITHelper {
// image files should be stored in numbered format like 1.png, 2.png, 3.png etc to work with ffmpeg
static Future<String> imagesToMp4(List<File> images) async {
final tempDir = await getTemporaryDirectory();
final createdOn = DateTime.now();
final fileName = createdOn.millisecondsSinceEpoch.toString();
final outputPath = '${tempDir.path}/$fileName.mp4';
final directory = '${images.first.parent.path}/%d.png';
final String ffmpegCommand = '-y -i $directory -qscale:v 1 $outputPath';
final session = await FFmpegKit.execute(
ffmpegCommand,
);
await session.getOutput();
final capturedVideo = XFile(outputPath);
return capturedVideo.path;
}
static Future<String> imagesToGif(List<File> images) async {
final tempDir = await getTemporaryDirectory();
final appDir = await getApplicationDocumentsDirectory();
final String outputPath = '${tempDir.path}/captured_gif.gif';
final String palletePath = '${appDir.path}/palette.png';
final directory = '${images.first.parent.path}/%d.png';
final generatePalette =
"-y -framerate 30 -i $directory -vf 'palettegen' $palletePath";
final session1 = await FFmpegKit.execute(
generatePalette,
);
final returnCode1 = await session1.getOutput();
dev.log(returnCode1.toString());
final String usePalette =
'-y -framerate 30 -i $directory -i $palletePath -lavfi "paletteuse" -qscale:v 1 $outputPath';
final session = await FFmpegKit.execute(
usePalette,
);
final returnCode = await session.getOutput();
dev.log(returnCode.toString());
final capturedGif = XFile(outputPath);
return capturedGif.path;
}
static Future<String> videoToGif(File mp4File, String outputPath) async {
final command =
"-y -i ${mp4File.path} -vf 'fps=25,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse' $outputPath";
final session = await FFmpegKit.execute(
command,
);
return outputPath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment