Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix framerate, new options #217

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,35 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin {
val duration = call.argument<Int>("duration")
val includeAudio = call.argument<Boolean>("includeAudio") ?: true
val frameRate = if (call.argument<Int>("frameRate")==null) 30 else call.argument<Int>("frameRate")
val bitrate = if (call.argument<Int>("bitrate")==null) DefaultVideoStrategy.BITRATE_UNKNOWN else call.argument<Int>("bitrate")

val tempDir: String = context.getExternalFilesDir("video_compress")!!.absolutePath
val out = SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(Date())
val destPath: String = tempDir + File.separator + "VID_" + out + path.hashCode() + ".mp4"

var videoTrackStrategy: TrackStrategy = DefaultVideoStrategy.atMost(340).build();
val audioTrackStrategy: TrackStrategy

when (quality) {

0 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(720).build()
videoTrackStrategy = DefaultVideoStrategy
.atMost(720)
.bitRate(bitrate!!.toLong())
.frameRate(frameRate!!).build()
}

1 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(360).build()
videoTrackStrategy = DefaultVideoStrategy
.atMost(360)
.bitRate(bitrate!!.toLong())
.frameRate(frameRate!!).build()
}
2 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(640).build()
videoTrackStrategy = DefaultVideoStrategy
.atMost(640)
.bitRate(bitrate!!.toLong())
.frameRate(frameRate!!).build()
}
3 -> {

assert(value = frameRate != null)
videoTrackStrategy = DefaultVideoStrategy.Builder()
.keyFrameInterval(3f)
Expand All @@ -113,20 +120,32 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin {
.build()
}
4 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(480, 640).build()
videoTrackStrategy = DefaultVideoStrategy
.atMost(480, 640)
.bitRate(bitrate!!.toLong())
.frameRate(frameRate!!).build()
}
5 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(540, 960).build()
videoTrackStrategy = DefaultVideoStrategy
.atMost(540, 960)
.bitRate(bitrate!!.toLong())
.frameRate(frameRate!!).build()
}
6 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(720, 1280).build()
videoTrackStrategy = DefaultVideoStrategy
.atMost(720, 1280)
.bitRate(bitrate!!.toLong())
.frameRate(frameRate!!).build()
}
7 -> {
videoTrackStrategy = DefaultVideoStrategy.atMost(1080, 1920).build()
videoTrackStrategy = DefaultVideoStrategy
.atMost(1080, 1920)
.bitRate(bitrate!!.toLong())
.frameRate(frameRate!!).build()
}
}

audioTrackStrategy = if (includeAudio) {
val audioTrackStrategy: TrackStrategy = if (includeAudio) {
val sampleRate = DefaultAudioStrategy.SAMPLE_RATE_AS_INPUT
val channels = DefaultAudioStrategy.CHANNELS_AS_INPUT

Expand Down
10 changes: 8 additions & 2 deletions ios/Classes/SwiftVideoCompressPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin {
let duration = args!["duration"] as? Double
let includeAudio = args!["includeAudio"] as? Bool
let frameRate = args!["frameRate"] as? Int
let fileLengthLimit = args!["fileLengthLimit"] as? Int
compressVideo(path, quality, deleteOrigin, startTime, duration, includeAudio,
frameRate, result)
frameRate, fileLengthLimit, result)
case "cancelCompression":
cancelCompression(result)
case "deleteAllCache":
Expand Down Expand Up @@ -175,7 +176,7 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin {
}

private func compressVideo(_ path: String,_ quality: NSNumber,_ deleteOrigin: Bool,_ startTime: Double?,
_ duration: Double?,_ includeAudio: Bool?,_ frameRate: Int?,
_ duration: Double?,_ includeAudio: Bool?,_ frameRate: Int?,_ fileLengthLimit: Int?,
_ result: @escaping FlutterResult) {
let sourceVideoUrl = Utility.getPathUrl(path)
let sourceVideoType = "mp4"
Expand Down Expand Up @@ -207,9 +208,14 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin {
exporter.outputURL = compressionUrl
exporter.outputFileType = AVFileType.mp4
exporter.shouldOptimizeForNetworkUse = true
exporter.canPerformMultiplePassesOverSourceMediaData = true
if fileLengthLimit != nil {
exporter.fileLengthLimit = Int64(fileLengthLimit!)
}

if frameRate != nil {
let videoComposition = AVMutableVideoComposition(propertiesOf: sourceVideoAsset)
videoComposition.sourceTrackIDForFrameTiming = kCMPersistentTrackID_Invalid;
videoComposition.frameDuration = CMTimeMake(value: 1, timescale: Int32(frameRate!))
exporter.videoComposition = videoComposition
}
Expand Down
28 changes: 28 additions & 0 deletions lib/src/video_compress/video_compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:video_compress/src/progress_callback/compress_mixin.dart';
import 'package:video_compress/video_compress.dart';

Expand Down Expand Up @@ -112,6 +113,9 @@ extension Compress on IVideoCompress {
/// determine whether to delete his source file by [deleteOrigin]
/// optional parameters [startTime] [duration] [includeAudio] [frameRate]
///
/// [fileLengthLimit] works only on ios
/// [bitrate] works only on android
///
/// ## example
/// ```dart
/// final info = await _flutterVideoCompress.compressVideo(
Expand All @@ -128,6 +132,8 @@ extension Compress on IVideoCompress {
int? duration,
bool? includeAudio,
int frameRate = 30,
IosOptions? iosOptions,
AndroidOptions? androidOptions,
}) async {
if (isCompressing) {
throw StateError('''VideoCompress Error:
Expand All @@ -150,6 +156,9 @@ extension Compress on IVideoCompress {
'duration': duration,
'includeAudio': includeAudio,
'frameRate': frameRate,
if (iosOptions?.fileLengthLimit != null)
'fileLengthLimit': iosOptions!.fileLengthLimit,
if (androidOptions?.bitrate != null) 'bitrate': androidOptions!.bitrate,
});

// ignore: invalid_use_of_protected_member
Expand Down Expand Up @@ -181,3 +190,22 @@ extension Compress on IVideoCompress {
});
}
}

class IosOptions {
/// The file length that the output of the session must not exceed.
/// https://developer.apple.com/documentation/avfoundation/avassetexportsession/1622333-filelengthlimit
final int? fileLengthLimit;
IosOptions({
this.fileLengthLimit,
});
}

class AndroidOptions {
/// Desired bit rate (bits per second). Can optionally be
/// null, in which case the strategy will try to estimate the bitrate.
/// https://natario1.github.io/Transcoder/docs/track-strategies#other-options
final int? bitrate;
AndroidOptions({
this.bitrate,
});
}