How to convert GIF to a mp4 video in Flutter? With flutter_ffmpeg or any other packages.
2 Answers
Method 1:
Using flutter_ffmpeg is really easy and you can do a lot of other things with the package.
To set up the package add this in pubspec.yaml
dependencies:
flutter_ffmpeg: ^0.4.2
add this in your build.gradle
located here android/build.gradle
ext {
flutterFFmpegPackage = "min"
}
finally in your code
import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';
...
...
final FlutterFFmpeg _flutterFFmpeg = FlutterFFmpeg();
final String inputFile = ".../input.gif"; //path of the gif file.
final String outputFile = ".../output.mp4"; //path to export the mp4 file.
await _flutterFFmpeg
.execute("-f gif -i $inputFile $outputFile")
.then((rc) => print("FFmpeg process 1 exited with rc $rc"));
Note: If you are exporting anywhere other than your app's directory, the file would have to be scanned or you wouldn't be able to use the file.
Method 2:
If all you wanna do is convert a .gif
to .mp4
then you can save a lot of space by just renaming .gif
to .mp4
and it'll work. You can also rename the same file from .mp4
to .gif
. But, this method wouldn't work if you wanna convert an original .mp4
file to .gif
.
Here's an example on how to rename a file:
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
//Let's assume that "$appDocPath/awesome.gif" is the path of the file.
await File("$appDocPath/awesome.gif").rename("$appDocPath/awesome.mp4");
NOTE : You have to enter the full path of the file in rename()
.
You can again rename the same file back to .gif
, and it would work. But, this method wouldn't work if you want to convert an .mp4
file that was never converted from a .gif
.

- 1,148
- 2
- 7
- 15
-
Thanks for the answer, but can you apply more details and code of how to **Rename** a file? – Ryan Wang Aug 21 '21 at 17:58
-
1I got this error while renaming it: `OS Error: No such file or directory, errno = 2` – Ryan Wang Aug 22 '21 at 20:42
-
are you sure you entered the correct path of the file? – Shaan Mephobic Aug 23 '21 at 10:22
-
Hi @ShaanMephobic, the mp4 file which converted from about way, it can not play, its totally black. Do you know why? Thanks. – Chuong Le Van Oct 10 '22 at 16:13
-
@ChuongLeVan Maybe this would have something to do with `Media Scanner` intent. Could you try this https://stackoverflow.com/a/69025529/14595863 ? – Shaan Mephobic Oct 11 '22 at 06:38
-
1I found out a way, just add this command "-f gif -i $inputFile -movflags faststart -pix_fmt yuv420p -vf 'scale=trunc(iw/2)*2:trunc(ih/2)*2' $outputFile" it should be work. – Chuong Le Van Oct 11 '22 at 14:13
-
```final gifExist = File(globals.gifFile!.path).existsSync(); if (gifExist) { debugPrint(globals.gifFile!.path); final file = await File(globals.gifFile!.path).rename('${globals.gifFile!.path}.mp4'); debugPrint(file.path); ImageGallerySaver.saveFile(file.path, isReturnPathOfIOS: true); }``` @ShaanMephobic always give me cannot be saved to the photo library: Error Domain=AVFoundationErrorDomain Code=-11829 (may be damaged) – iOS Flow Feb 18 '23 at 15:49
-
note my gif is in getApplicationDocumentsDirectory ```Directory documentDirectory = await getApplicationDocumentsDirectory(); File file = File(path.join(documentDirectory.path, path.basename('tempGif'))); await file.writeAsBytes(globals.tempGif!);``` any idea?? @ShaanMephobic? – iOS Flow Feb 18 '23 at 15:54
-
Maaaan hours on that... Couldn't make the mp4 from GIF to use for ImageGallerySaver. It was always failing - tried to rename, tried to duplicate with different name (.mp4) - no way to make it work. But anyway having mp4 which doesn't loop would not even be satisfactory. And I wasn't trusting the legend saying we cannot save gif to camera roll as whatsapp does - no problem. Then this morning after 15' in I had the brilliant idea to give up on ImageGallerySaver but rather use **share_plus** - Job done with no pain... – iOS Flow Feb 19 '23 at 08:21