Merge pull request #523 from GetStream/feat/videoCompressOptions

feat: add video compress options (frame and quality) to message input [CDS-343]
This commit is contained in:
Salvatore Giordano
2021-07-07 16:18:45 +02:00
committed by GitHub
2 changed files with 29 additions and 9 deletions
@@ -25,6 +25,8 @@ import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart';
import 'package:substring_highlight/substring_highlight.dart'; import 'package:substring_highlight/substring_highlight.dart';
import 'package:video_compress/video_compress.dart'; import 'package:video_compress/video_compress.dart';
export 'package:video_compress/video_compress.dart' show VideoQuality;
/// Builder for attachment thumbnails /// Builder for attachment thumbnails
typedef AttachmentThumbnailBuilder = Widget Function( typedef AttachmentThumbnailBuilder = Widget Function(
BuildContext, BuildContext,
@@ -149,11 +151,19 @@ class MessageInput extends StatefulWidget {
this.showCommandsButton = true, this.showCommandsButton = true,
this.mentionsTileBuilder, this.mentionsTileBuilder,
this.maxAttachmentSize = _kDefaultMaxAttachmentSize, this.maxAttachmentSize = _kDefaultMaxAttachmentSize,
this.compressedVideoQuality = VideoQuality.DefaultQuality,
this.compressedVideoFrameRate = 30,
}) : super(key: key); }) : super(key: key);
/// Message to edit /// Message to edit
final Message? editMessage; final Message? editMessage;
/// Video quality to use when compressing the videos
final VideoQuality compressedVideoQuality;
/// Frame rate to use when compressing the videos
final int compressedVideoFrameRate;
/// Max attachment size in bytes /// Max attachment size in bytes
/// Defaults to 20 MB /// Defaults to 20 MB
/// do not set it if you're using our default CDN /// do not set it if you're using our default CDN
@@ -1116,8 +1126,11 @@ class MessageInputState extends State<MessageInput> {
if (file.size! > widget.maxAttachmentSize) { if (file.size! > widget.maxAttachmentSize) {
if (medium.type == AssetType.video && file.path != null) { if (medium.type == AssetType.video && file.path != null) {
final mediaInfo = await (VideoService.compressVideo(file.path!) final mediaInfo = await (VideoService.compressVideo(
as FutureOr<MediaInfo>); file.path!,
frameRate: widget.compressedVideoFrameRate,
quality: widget.compressedVideoQuality,
) as FutureOr<MediaInfo>);
if (mediaInfo.filesize! > widget.maxAttachmentSize) { if (mediaInfo.filesize! > widget.maxAttachmentSize) {
_showErrorAlert( _showErrorAlert(
@@ -1892,8 +1905,11 @@ class MessageInputState extends State<MessageInput> {
if (file.size! > widget.maxAttachmentSize) { if (file.size! > widget.maxAttachmentSize) {
if (attachmentType == 'video' && file.path != null) { if (attachmentType == 'video' && file.path != null) {
final mediaInfo = await (VideoService.compressVideo(file.path!) final mediaInfo = await (VideoService.compressVideo(
as FutureOr<MediaInfo>); file.path!,
frameRate: widget.compressedVideoFrameRate,
quality: widget.compressedVideoQuality,
) as FutureOr<MediaInfo>);
if (mediaInfo.filesize! > widget.maxAttachmentSize) { if (mediaInfo.filesize! > widget.maxAttachmentSize) {
_showErrorAlert( _showErrorAlert(
@@ -16,21 +16,25 @@ class IVideoService {
/// compress video from [path] /// compress video from [path]
/// compress video from [path] return [Future<MediaInfo>] /// compress video from [path] return [Future<MediaInfo>]
/// ///
/// you can choose its quality by [quality], /// you can choose its [quality] and [frameRate]
/// determine whether to delete his source file by [deleteOrigin]
/// optional parameters [startTime] [duration] [includeAudio] [frameRate]
/// ///
/// ## example /// ## example
/// ```dart /// ```dart
/// final info = await _flutterVideoCompress.compressVideo( /// final info = await _flutterVideoCompress.compressVideo(
/// file.path, /// file.path,
/// deleteOrigin: true,
/// ); /// );
/// debugPrint(info.toJson()); /// debugPrint(info.toJson());
/// ``` /// ```
Future<MediaInfo?> compressVideo(String path) async => _lock.synchronized( Future<MediaInfo?> compressVideo(
String path, {
int frameRate = 30,
VideoQuality quality = VideoQuality.DefaultQuality,
}) async =>
_lock.synchronized(
() => VideoCompress.compressVideo( () => VideoCompress.compressVideo(
path, path,
frameRate: frameRate,
quality: quality,
), ),
); );