wy_video_compress 3.1.4+5
wy_video_compress: ^3.1.4+5 copied to clipboard
Generate a new path by compressed video, Choose to keep the source video or delete it by a parameter. Get video thumbnail from a video path and provide video information. Easy to deal with compressed video.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:wy_video_compress/wy_video_compress.dart';
import 'package:file_selector/file_selector.dart';
import 'dart:io';
import 'package:video_compress_example/video_thumbnail.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:appinio_video_player/appinio_video_player.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _before_info = '[Original video information]';
String _after_info = '[Compressed video information]';
late VideoPlayerController _videoPlayerController = VideoPlayerController.networkUrl(Uri.parse(''));
late CustomVideoPlayerController _customVideoPlayerController = CustomVideoPlayerController(
context: context,
videoPlayerController: _videoPlayerController,
);
String playPath = "";
// Subscription _subscription = VideoCompress.compressProgress$.subscribe((progress) {
// print("progress:$progress");
// });
Future<void> _compressVideo() async {
var file;
if (Platform.isMacOS) {
final typeGroup = XTypeGroup(label: 'videos', extensions: ['mov', 'mp4']);
file = await openFile(acceptedTypeGroups: [typeGroup]);
} else {
final List<AssetEntity>? result = await AssetPicker.pickAssets(
context,
pickerConfig: const AssetPickerConfig(requestType: RequestType.video, maxAssets: 1),
);
if (result != null && result.length > 0) {
AssetEntity entity = result[0];
file = await entity.file;
}
// final picker = ImagePicker();
// var pickedFile = await picker.pickVideo(source: ImageSource.gallery);
// file = File(pickedFile!.path);
}
if (file == null) {
return;
}
await VideoCompress.setLogLevel(0);
final b_info = await VideoCompress.getMediaInfo(file.path);
var str = '[Original video information] info: ${b_info.toJson()}\n';
print(str);
setState(() {
_before_info = str;
});
//bitrate = filesize * 8 / duration * 1000
final info = await VideoCompress.compressVideo(
file.path,
quality: VideoQuality.HighestQuality,
deleteOrigin: false,
includeAudio: true,
frameRate: 24,
bitRate: 17448464,
width: b_info.width! ~/ 2,
height: b_info.height! ~/ 2,
startTime: 0,
duration: 3,
);
if (info != null) {
playPath = info.path!;
var str = '[Compressed video information] info: ${info.toJson()}\n';
print(str);
setState(() {
_after_info = str;
});
}
}
Future<void> _saveVideoToAlbum() async {
if (!playPath.isEmpty) {
await VideoCompress.saveVideoToAlbum(playPath);
VideoCompress.deleteAllCache();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: ListView(
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_before_info',
style: Theme.of(context).textTheme.titleMedium,
),
Text(
'$_after_info',
style: Theme.of(context).textTheme.titleMedium,
),
InkWell(
child: Icon(
Icons.cancel,
size: 55,
),
onTap: () {
VideoCompress.cancelCompression();
}),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => VideoThumbnail()),
);
},
child: Text('Test thumbnail'),
),
AspectRatio(
aspectRatio: _videoPlayerController.value.aspectRatio,
child: CustomVideoPlayer(
customVideoPlayerController: _customVideoPlayerController
),
),
ElevatedButton(
onPressed: () {
if (_videoPlayerController.value.isPlaying) {
_videoPlayerController.pause();
} else {
_videoPlayerController = VideoPlayerController.file(File(playPath))..initialize().then((_) {
setState(() {});
});
_customVideoPlayerController = CustomVideoPlayerController(
context: context,
videoPlayerController: _videoPlayerController,
);
_videoPlayerController.play();
}
},
child: Text('Compressed Video Play'),
),
ElevatedButton(
onPressed: () => _saveVideoToAlbum(),
child: Text('Save Album'),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () async => _compressVideo(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}