compressVideo static method
Future<String?>
compressVideo({
- required String inputPath,
- int bitrate = 2000000,
- int? width,
- int? height,
- VideoSetting videoSetting = VideoSetting.h264,
- AudioSetting audioSetting = AudioSetting.aac,
- int audioBitrate = 128000,
- int audioSampleRate = 44100,
- int audioChannels = 2,
- bool printingInfo = false,
Compress video
inputPath : Input video path
outputPath : Output video path
bitrate : Video bitrate (e.g. 2000000 = 2Mbps)
width : Output video width
height : Output video height
videoCodec : Video codec ("h264" or "h265"/"hevc"), default: "h264"
audioCodec : Audio codec ("aac", "alac", "mp3"), default: "aac"
audioBitrate : Audio bitrate (e.g. 128000 = 128kbps), default: 128000
audioSampleRate : Audio sample rate (e.g. 44100), default: 44100
audioChannels : Audio channels (1=mono, 2=stereo), default: 2
printingInfo : Print video info, default: false
Implementation
static Future<String?> compressVideo({
required String inputPath,
int bitrate = 2000000,
int? width,
int? height,
VideoSetting videoSetting = VideoSetting.h264,
AudioSetting audioSetting = AudioSetting.aac,
int audioBitrate = 128000,
int audioSampleRate = 44100,
int audioChannels = 2,
bool printingInfo = false,
}) async {
try {
if (printingInfo) {
debugPrint(
'-------------------------------- Before compress video info --------------------------------',
);
await printVideoInfo(inputPath);
}
/// Output path creation
final dir = await getTemporaryDirectory();
final outputPath = '${dir.path}/$outputFileName';
await _channel.invokeMethod('compressVideo', {
'input': inputPath,
'output': outputPath,
'bitrate': bitrate,
'width': width,
'height': height,
'videoCodec': videoSetting.value,
'audioCodec': audioSetting.value,
'audioBitrate': audioBitrate,
'audioSampleRate': audioSampleRate,
'audioChannels': audioChannels,
});
if (printingInfo) {
debugPrint(
'-------------------------------- After compress video info --------------------------------',
);
await printVideoInfo(outputPath);
}
return outputPath;
} on PlatformException catch (e) {
debugPrint('Failed to compress video: $e');
return null;
}
}