Reduce your Dart/Flutter app size.
A simple package that goes through every asset in your project's assets folders and optimizes them using common compression techniques. Handles images, video, and audio files in a variety of formats.
Features
- Compress images, video, and audio in one pass
- Compress all assets, choose what to skip, or preview savings with a dry run
- Tune quality (
--quality) or optimize just one file (--single-file) - Optional output folder keeps your originals untouched
- Keeps each file in its original format
- Includes Android and iOS resources and assets
- No extra setup on Windows/macOS; Linux uses your installed ffmpeg
Supported formats
Image
| Format | Windows | macOS | Linux |
|---|---|---|---|
jpg, jpeg |
✅️ | ✅️ | ✅️ |
png |
✅️ | ✅️ | ✅️ |
gif |
✅️ | ✅️ | ✅️ |
webp |
✅️ | ✅️ | ✅️ |
tif, tiff |
✅️ | ✅️ | ✅️ |
bmp |
✅️ | ✅️ | ✅️ |
ico |
✅️ | ✅️ | ✅️ |
svg |
❌️ | ❌️ | ❌️ |
Video
| Format | Windows | macOS | Linux |
|---|---|---|---|
mp4 |
✅️ | ✅️ | ✅️ |
m4v |
✅️ | ✅️ | ✅️ |
mov |
✅️ | ✅️ | ✅️ |
3gp |
✅️ | ❌️ | ✅️ |
3g2 |
✅️ | ❌️ | ✅️ |
webm |
✅️ | ✅️ | ✅️ |
avi |
❌️ | ❌️ | ✅️ |
mkv |
❌️ | ❌️ | ✅️ |
Audio
| Format | Windows | macOS | Linux |
|---|---|---|---|
m4a |
✅️ | ✅️ | ✅️ |
mp3 |
✅️ | ✅️ | ✅️ |
ogg |
✅️ | ✅️ | ✅️ |
opus |
✅️ | ✅️ | ✅️ |
wav |
✅️ | ✅️ | ✅️ |
aac |
✅️ | ✅️ | ✅️ |
How it works
Video files are reencoded into H.264 + MP3 or VP9 + Opus. Audio bitrate, sample rate, and bit depth are reduced where the format allows (for example, MP3 defaults to 128kbps, 44kHz, 16-bit). This is usually enough to reduce the size of most media files substantially while maintaining quality. Use the --quality flag to override these defaults. Default quality is 80
Requirements
Development platform
Windows, macOS
If you are developing on Windows or macOS, the package uses the in-built multimedia processing libraries MediaFoundation and AVFoundation respectively, so no setup is needed. Just add the package and run.
Linux
On Linux, since there are so many distros and configurations, an in-built multimedia library may not always be available. Therefore, the package relies on a user-installed ffmpeg binary. You can download it here and add it to the PATH environment variable, or pass --ffmpeg-path as a CLI arg.
Usage
Add the package as a dev dependency:
dev_dependencies:
media_asset_optimizer: ^1.0.0
Then run it from your project root:
dart run media_asset_optimizer
When run without a flag, an interactive CLI is shown. Alternatively, you can run it with flags:
dart run media_asset_optimizer --all # compress every media asset
dart run media_asset_optimizer --select # choose which assets to skip. Compress others
dart run media_asset_optimizer --dry-run # shows which files will be affected
Some useful extras:
dart run media_asset_optimizer -q 80 # quality: 1 = smallest, 100 = best, 80 = default
dart run media_asset_optimizer -f assets/audio/alarm.mp3 # compress one file
dart run media_asset_optimizer -o build/optimized # save to a folder inside your project
dart run media_asset_optimizer -p /usr/bin/ffmpeg # Linux: point to ffmpeg if it isn't on PATH
Run dart run media_asset_optimizer --help for the full list of flags.
Note: Ensure that you use a backup/version control system so that the changes can be reversed if needed. Alternatively, you can pass an --output-path where the outputs will be stored, and prevent overwriting the originals.
Motivation
Android performs limited image optimization during a release build, but audio and video files are untouched. iOS does not perform optimization on any kind of media during App Store builds. It's easy to lose track of asset size as a project grows, but you can usually gain some good savings by simply compressing them.
Recently, I was able to cut down the size of one of my apps by 18% by manually compressing audio files (used for alarm ringtones). This led me to build this package to automate the process for my own needs as well as others'. Hopefully it helps a fellow dev!
Contributing
Contributions are welcome. Please see CONTRIBUTING.md if you would like to contribute to the project.
Third-party licenses
This project uses lame, libvpx, libwebm, libopus, libogg, and libvorbis. The third-party notice, LGPL text, and written offer for source code are included in the project docs.
Acknowledgement
The interactive list TUI code in src/ui is adapted from interact_cli by @payam-zahedi, licensed under the MIT license. Many thanks to the maintainers of this awesome project.