I need to be able to rotate a video in Flutter without re-encoding it.
In Flutter I’m using the ffmpeg_kit_flutter_new 4.1.0 plugin, and for the rotation I’m trying the following command:
ffmpeg -i input.mp4 -map_metadata 0 -metadata:s:v rotate="90" -codec copy output.mp4
This command doesn’t work for me either in the app or in the macOS version.
I discovered that changing the display_rotation property actually works, but this approach is not compatible with the Flutter version of FFmpeg 7.x.
The command that works on macOS is:
ffmpeg -display_rotation 90 -i input.mp4 -c copy output.mp4
This really rotates the video on macOS without re-encoding, but when I apply this in Flutter…
Future<void> saveVideo(XFile file) async {
try {
final tempDir = await getTemporaryDirectory();
final inputPath =
'${tempDir.path}/${DateTime.now().millisecondsSinceEpoch}_input.mp4';
await File(file.path).copy(inputPath);
final outputPath =
'${tempDir.path}/${DateTime.now().millisecondsSinceEpoch}_rotated.mp4';
final cmd =
'-y -display_rotation 90 -i "$inputPath" -c copy "$outputPath"';
final session = await FFmpegKit.execute(cmd);
final returnCode = await session.getReturnCode();
final logs = await session.getAllLogs();
for (final log in logs) {
_logger.info('FFmpeg', log.getMessage());
}
if (!ReturnCode.isSuccess(returnCode)) {
_logger.error(
'GalleryService',
'Error setting display_rotation metadata: $returnCode',
);
throw Exception('Error setting display_rotation metadata');
}
final wasSuccess = await ImageGallerySaverPlus.saveFile(
outputPath,
).then(_mapSavingResult);
if (!wasSuccess) {
_logger.error(
'GalleryService',
'Error saving video in gallery',
);
throw Exception('Error saving video in gallery');
}
} catch (error) {
_logger.error(
'GalleryService',
'Error saving video in gallery: $error',
);
throw Exception('Error saving video in gallery');
}
}
The output of this is
I/flutter (12698): │ 💬 Display transformation (rotation/flip) not supported with FFmpeg 7.x -
I/flutter (12698): │ 💬 displaymatrix: rotation of -90.00 degrees
Other way of rotate the video is with exiftool but again i only found this for Desktop devices...
exiftool -m -P -api LargeFileSupport=1 -rotation=90 -o output.mp4 landscape_right.mp4
So i need a fast and performance way for changing the video rotation in metadata.
Any help would be appreciated