convertImageToVideo static method
Implementation
static Future<File> convertImageToVideo({
required File imageFile,
required Duration duration,
}) async {
final directory = await getTemporaryDirectory();
final timestamp = DateTime.now().millisecondsSinceEpoch;
final outputFile = File('${directory.path}/image_clip_$timestamp.mp4');
final seconds = (duration.inMilliseconds / 1000)
.clamp(0.5, 3600)
.toStringAsFixed(3);
final codec = Platform.isIOS ? 'h264_videotoolbox' : 'libx264';
final vSettings = Platform.isIOS ? ['-b:v', '5M'] : ['-preset', 'veryfast'];
// Keeps the image's own aspect ratio (only rounded to even dimensions);
// the project render fits it to the canvas.
final session = await FFmpegKit.executeWithArguments([
'-y',
'-loop', '1',
'-framerate', '30',
'-t', seconds,
'-i', imageFile.path,
'-f', 'lavfi', '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100',
'-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2,setsar=1,format=yuv420p',
'-r', '30',
'-c:v', codec,
...vSettings,
'-c:a', 'aac',
'-shortest',
'-movflags', '+faststart',
outputFile.path,
]);
final returnCode = await session.getReturnCode();
if (!ReturnCode.isSuccess(returnCode)) {
final logs = await session.getLogsAsString();
throw Exception('Image to video conversion failed: $logs');
}
return outputFile;
}