| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:flutter_ffmpeg/flutter_ffmpeg.dart';
- import 'package:flutter_ffmpeg/log_level.dart';
- import 'package:flutter_ffmpeg_example/flutter_ffmpeg_test_app.dart';
- import 'package:flutter_ffmpeg_example/video_util.dart';
- class FlutterFFmpegTestAppState extends State<MainPage> with TickerProviderStateMixin {
- static const String ASSET_1 = "1.jpg";
- static const String ASSET_2 = "2.jpg";
- static const String ASSET_3 = "3.jpg";
- final FlutterFFmpeg _flutterFFmpeg = new FlutterFFmpeg();
- TextEditingController _commandController;
- TabController _controller;
- String _commandOutput;
- String _encodeOutput;
- String _currentCodec;
- List<DropdownMenuItem<String>> _codecDropDownMenuItems;
- @override
- void initState() {
- super.initState();
- _commandController = TextEditingController();
- _controller = TabController(length: 2, vsync: this);
- _commandOutput = "";
- _encodeOutput = "";
- _codecDropDownMenuItems = _getCodecDropDownMenuItems();
- _currentCodec = _codecDropDownMenuItems[0].value;
- startupTests();
- prepareAssets();
- }
- void startupTests() {
- getFFmpegVersion().then((version) => print("FFmpeg version: $version"));
- getPlatform().then((platform) => print("Platform: $platform"));
- getLogLevel().then((level) => print("Old log level: " + LogLevel.levelToString(level)));
- setLogLevel(LogLevel.AV_LOG_INFO);
- getLogLevel().then((level) => print("New log level: " + LogLevel.levelToString(level)));
- getPackageName().then((packageName) => print("Package name: $packageName"));
- getExternalLibraries().then((packageList) {
- packageList.forEach((value) => print("External library: $value"));
- });
- }
- void prepareAssets() {
- VideoUtil.copyFileAssets('assets/pyramid.jpg', ASSET_1).then((path) => print('Loaded asset $path.'));
- VideoUtil.copyFileAssets('assets/colosseum.jpg', ASSET_2).then((path) => print('Loaded asset $path.'));
- VideoUtil.copyFileAssets('assets/tajmahal.jpg', ASSET_3).then((path) => print('Loaded asset $path.'));
- }
- void testRunCommand() {
- getLastReturnCode().then((rc) => print("Last rc: $rc"));
- getLastCommandOutput().then((output) => print("Last command output: $output"));
- print("Testing COMMAND.");
- // ENABLE LOG CALLBACK ON EACH CALL
- enableLogCallback(commandOutputLogCallback);
- enableStatisticsCallback(statisticsCallback);
- // CLEAR OUTPUT ON EACH EXECUTION
- _commandOutput = "";
- // COMMENT OPTIONAL TESTS
- // VideoUtil.tempDirectory.then((tempDirectory) {
- // Map<String, String> mapNameMap = new Map();
- // mapNameMap["my_custom_font"] = "my custom font";
- // setFontDirectory(tempDirectory.path, null);
- // });
- VideoUtil.tempDirectory.then((tempDirectory) {
- setFontconfigConfigurationPath(tempDirectory.path);
- });
- // disableRedirection();
- // disableLogs();
- // enableLogs();
- // execute(_commandController.text).then((rc) => print("FFmpeg process exited with rc $rc"));
- // executeWithDelimiter(_commandController.text, "_").then((rc) => print("FFmpeg process exited with rc $rc") );
- executeWithArguments(_commandController.text.split(" ")).then((rc) => print("FFmpeg process exited with rc $rc"));
- setState(() {});
- }
- void testGetMediaInformation(String mediaPath) {
- print("Testing Get Media Information.");
- // ENABLE LOG CALLBACK ON EACH CALL
- enableLogCallback(commandOutputLogCallback);
- enableStatisticsCallback(null);
- // CLEAR OUTPUT ON EACH EXECUTION
- _commandOutput = "";
- VideoUtil.assetPath(mediaPath).then((image1Path) {
- getMediaInformation(image1Path).then((info) {
- print('Media Information');
- print('Path: ${info['path']}');
- print('Format: ${info['format']}');
- print('Duration: ${info['duration']}');
- print('Start time: ${info['startTime']}');
- print('Bitrate: ${info['bitrate']}');
- if (info['streams'] != null) {
- final streamsInfoArray = info['streams'];
- if (streamsInfoArray.length > 0) {
- for (var streamsInfo in streamsInfoArray) {
- print('Stream id: ${streamsInfo['index']}');
- print('Stream type: ${streamsInfo['type']}');
- print('Stream codec: ${streamsInfo['codec']}');
- print('Stream full codec: ${streamsInfo['fullCodec']}');
- print('Stream format: ${streamsInfo['format']}');
- print('Stream full format: ${streamsInfo['fullFormat']}');
- print('Stream width: ${streamsInfo['width']}');
- print('Stream height: ${streamsInfo['height']}');
- print('Stream bitrate: ${streamsInfo['bitrate']}');
- print('Stream sample rate: ${streamsInfo['sampleRate']}');
- print('Stream sample format: ${streamsInfo['sampleFormat']}');
- print('Stream channel layout: ${streamsInfo['channelLayout']}');
- print('Stream sar: ${streamsInfo['sampleAspectRatio']}');
- print('Stream dar: ${streamsInfo['displayAspectRatio']}');
- print('Stream average frame rate: ${streamsInfo['averageFrameRate']}');
- print('Stream real frame rate: ${streamsInfo['realFrameRate']}');
- print('Stream time base: ${streamsInfo['timeBase']}');
- print('Stream codec time base: ${streamsInfo['codecTimeBase']}');
- }
- }
- }
- });
- });
- setState(() {});
- }
- void testEncodeVideo() {
- print("Testing VIDEO.");
- // ENABLE LOG CALLBACK ON EACH CALL
- enableLogCallback(encodeOutputLogCallback);
- enableStatisticsCallback(statisticsCallback);
- // CLEAR OUTPUT ON EACH EXECUTION
- _encodeOutput = "";
- disableStatistics();
- enableStatistics();
- VideoUtil.assetPath(ASSET_1).then((image1Path) {
- VideoUtil.assetPath(ASSET_2).then((image2Path) {
- VideoUtil.assetPath(ASSET_3).then((image3Path) {
- final String videoPath = getVideoPath();
- final String customOptions = getCustomEncodingOptions();
- final String ffmpegCodec = getFFmpegCodecName();
- VideoUtil.assetPath(videoPath).then((fullVideoPath) {
- execute(VideoUtil.generateEncodeVideoScript(image1Path, image2Path, image3Path, fullVideoPath, ffmpegCodec, customOptions)).then((rc) {
- if (rc == 0) {
- testGetMediaInformation(fullVideoPath);
- }
- });
- // COMMENT OPTIONAL TESTS
- // execute(VideoUtil.generateEncodeVideoScript(image1Path, image2Path, image3Path, videoPath, _currentCodec, "")).timeout(Duration(milliseconds: 1300), onTimeout: () { cancel(); });
- // resetStatistics();
- getLastReceivedStatistics().then((lastStatistics) {
- if (lastStatistics == null) {
- print('No last statistics');
- } else {
- print('Last statistics');
- int time = lastStatistics['time'];
- int size = lastStatistics['size'];
- double bitrate = _doublePrecision(lastStatistics['bitrate'], 2);
- double speed = _doublePrecision(lastStatistics['speed'], 2);
- int videoFrameNumber = lastStatistics['videoFrameNumber'];
- double videoQuality = _doublePrecision(lastStatistics['videoQuality'], 2);
- double videoFps = _doublePrecision(lastStatistics['videoFps'], 2);
- statisticsCallback(time, size, bitrate, speed, videoFrameNumber, videoQuality, videoFps);
- }
- });
- });
- });
- });
- });
- setState(() {});
- }
- void commandOutputLogCallback(int level, String message) {
- _commandOutput += message;
- setState(() {});
- }
- void encodeOutputLogCallback(int level, String message) {
- _encodeOutput += message;
- setState(() {});
- }
- void statisticsCallback(int time, int size, double bitrate, double speed, int videoFrameNumber, double videoQuality, double videoFps) {
- print("Statistics: time: $time, size: $size, bitrate: $bitrate, speed: $speed, videoFrameNumber: $videoFrameNumber, videoQuality: $videoQuality, videoFps: $videoFps");
- }
- Future<String> getFFmpegVersion() async {
- return await _flutterFFmpeg.getFFmpegVersion();
- }
- Future<String> getPlatform() async {
- return await _flutterFFmpeg.getPlatform();
- }
- Future<int> executeWithArguments(List arguments) async {
- return await _flutterFFmpeg.executeWithArguments(arguments);
- }
- Future<int> execute(String command) async {
- return await _flutterFFmpeg.execute(command);
- }
- Future<int> executeWithDelimiter(String command, String delimiter) async {
- return await _flutterFFmpeg.execute(command, delimiter);
- }
- Future<void> cancel() async {
- return await _flutterFFmpeg.cancel();
- }
- Future<void> disableRedirection() async {
- return await _flutterFFmpeg.disableRedirection();
- }
- Future<int> getLogLevel() async {
- return await _flutterFFmpeg.getLogLevel();
- }
- Future<void> setLogLevel(int logLevel) async {
- return await _flutterFFmpeg.setLogLevel(logLevel);
- }
- Future<void> enableLogs() async {
- return await _flutterFFmpeg.enableLogs();
- }
- Future<void> disableLogs() async {
- return await _flutterFFmpeg.disableLogs();
- }
- Future<void> enableStatistics() async {
- return await _flutterFFmpeg.enableStatistics();
- }
- Future<void> disableStatistics() async {
- return await _flutterFFmpeg.disableStatistics();
- }
- Future<void> enableLogCallback(Function(int level, String message) logCallback) async {
- await _flutterFFmpeg.enableLogCallback(logCallback);
- }
- Future<void> enableStatisticsCallback(Function(int time, int size, double bitrate, double speed, int videoFrameNumber, double videoQuality, double videoFps) statisticsCallback) async {
- await _flutterFFmpeg.enableStatisticsCallback(statisticsCallback);
- }
- Future<Map<dynamic, dynamic>> getLastReceivedStatistics() async {
- return await _flutterFFmpeg.getLastReceivedStatistics();
- }
- Future<void> resetStatistics() async {
- return await _flutterFFmpeg.resetStatistics();
- }
- Future<void> setFontconfigConfigurationPath(String path) async {
- return await _flutterFFmpeg.setFontconfigConfigurationPath(path);
- }
- Future<void> setFontDirectory(String fontDirectory, Map<String, String> fontNameMap) async {
- return await _flutterFFmpeg.setFontDirectory(fontDirectory, fontNameMap);
- }
- Future<String> getPackageName() async {
- return await _flutterFFmpeg.getPackageName();
- }
- Future<List<dynamic>> getExternalLibraries() async {
- return await _flutterFFmpeg.getExternalLibraries();
- }
- Future<int> getLastReturnCode() async {
- return await _flutterFFmpeg.getLastReturnCode();
- }
- Future<String> getLastCommandOutput() async {
- return await _flutterFFmpeg.getLastCommandOutput();
- }
- Future<Map<dynamic, dynamic>> getMediaInformation(String path) async {
- return await _flutterFFmpeg.getMediaInformation(path);
- }
- void _changedCodec(String selectedCodec) {
- setState(() {
- _currentCodec = selectedCodec;
- });
- }
- String getFFmpegCodecName() {
- String ffmpegCodec = _currentCodec;
- // VIDEO CODEC MENU HAS BASIC NAMES, FFMPEG NEEDS LONGER LIBRARY NAMES.
- if (ffmpegCodec == "h264") {
- ffmpegCodec = "libx264";
- } else if (ffmpegCodec == "x265") {
- ffmpegCodec = "libx265";
- } else if (ffmpegCodec == "xvid") {
- ffmpegCodec = "libxvid";
- } else if (ffmpegCodec == "vp8") {
- ffmpegCodec = "libvpx";
- } else if (ffmpegCodec == "vp9") {
- ffmpegCodec = "libvpx-vp9";
- }
- return ffmpegCodec;
- }
- String getVideoPath() {
- String ffmpegCodec = _currentCodec;
- String videoPath;
- if ((ffmpegCodec == "vp8") || (ffmpegCodec == "vp9")) {
- videoPath = "video.webm";
- } else {
- // mpeg4, x264, x265, xvid
- videoPath = "video.mp4";
- }
- return videoPath;
- }
- String getCustomEncodingOptions() {
- String videoCodec = _currentCodec;
- if (videoCodec == "x265") {
- return "-crf 28 -preset fast ";
- } else if (videoCodec == "vp8") {
- return "-b:v 1M -crf 10 ";
- } else if (videoCodec == "vp9") {
- return "-b:v 2M ";
- } else {
- return "";
- }
- }
- List<DropdownMenuItem<String>> _getCodecDropDownMenuItems() {
- List<DropdownMenuItem<String>> items = new List();
- items.add(new DropdownMenuItem(value: "mpeg4", child: new Text("mpeg4")));
- items.add(new DropdownMenuItem(value: "h264", child: new Text("h264")));
- items.add(new DropdownMenuItem(value: "x265", child: new Text("x265")));
- items.add(new DropdownMenuItem(value: "xvid", child: new Text("xvid")));
- items.add(new DropdownMenuItem(value: "vp8", child: new Text("vp8")));
- items.add(new DropdownMenuItem(value: "vp9", child: new Text("vp9")));
- return items;
- }
- double _doublePrecision(double value, int precision) {
- if (value == null) {
- return 0;
- } else {
- return num.parse(value.toStringAsFixed(precision));
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('FlutterFFmpeg Test'),
- centerTitle: true,
- ),
- bottomNavigationBar: Material(
- child: DecoratedTabBar(
- tabBar: TabBar(
- tabs: <Tab>[
- Tab(text: "COMMAND"),
- Tab(
- text: "VIDEO",
- )
- ],
- controller: _controller,
- labelColor: Color(0xFF1e90ff),
- unselectedLabelColor: Color(0xFF808080),
- ),
- decoration: BoxDecoration(
- border: Border(
- top: BorderSide(
- color: Color(0xFF808080),
- width: 1.0,
- ),
- bottom: BorderSide(
- width: 0.0,
- ),
- ),
- ),
- ),
- ),
- body: TabBarView(
- children: <Widget>[
- Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- Container(
- padding: const EdgeInsets.fromLTRB(20, 40, 20, 40),
- child: TextField(
- controller: _commandController,
- decoration: InputDecoration(
- border: const OutlineInputBorder(
- borderSide: const BorderSide(color: Color(0xFF3498DB)),
- borderRadius: const BorderRadius.all(
- const Radius.circular(5),
- ),
- ),
- focusedBorder: const OutlineInputBorder(
- borderSide: const BorderSide(color: Color(0xFF3498DB)),
- borderRadius: const BorderRadius.all(
- const Radius.circular(5),
- ),
- ),
- enabledBorder: const OutlineInputBorder(
- borderSide: const BorderSide(color: Color(0xFF3498DB)),
- borderRadius: const BorderRadius.all(
- const Radius.circular(5),
- ),
- ),
- contentPadding: EdgeInsets.fromLTRB(8, 12, 8, 12),
- hintStyle: new TextStyle(fontSize: 14, color: Colors.grey[400]),
- hintText: "Enter command"),
- style: new TextStyle(fontSize: 14, color: Colors.black),
- ),
- ),
- Container(
- padding: const EdgeInsets.only(bottom: 20),
- child: new InkWell(
- onTap: () => testRunCommand(),
- child: new Container(
- width: 100,
- height: 38,
- decoration: new BoxDecoration(
- color: Color(0xFF2ECC71),
- borderRadius: new BorderRadius.circular(5),
- ),
- child: new Center(
- child: new Text(
- 'RUN',
- style: new TextStyle(fontSize: 14.0, fontWeight: FontWeight.bold, color: Colors.white),
- ),
- ),
- ),
- ),
- ),
- Expanded(
- child: Container(
- alignment: Alignment(-1.0, -1.0),
- margin: EdgeInsets.all(20.0),
- padding: EdgeInsets.all(4.0),
- decoration: new BoxDecoration(borderRadius: BorderRadius.all(new Radius.circular(5)), color: Color(0xFFF1C40F)),
- child: SingleChildScrollView(reverse: true, child: Text(_commandOutput))),
- )
- ],
- ),
- Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- Container(
- padding: const EdgeInsets.fromLTRB(20, 40, 20, 40),
- child: Container(
- width: 200,
- alignment: Alignment(0.0, 0.0),
- decoration: BoxDecoration(color: Color.fromRGBO(155, 89, 182, 1.0), borderRadius: BorderRadius.circular(5)),
- child: DropdownButtonHideUnderline(
- child: DropdownButton(
- style: new TextStyle(
- fontSize: 14,
- color: Colors.black,
- ),
- value: _currentCodec,
- items: _codecDropDownMenuItems,
- onChanged: _changedCodec,
- iconSize: 0,
- isExpanded: false,
- )),
- )),
- Container(
- padding: const EdgeInsets.only(bottom: 20),
- child: new InkWell(
- onTap: () => testEncodeVideo(),
- child: new Container(
- width: 100,
- height: 38,
- decoration: new BoxDecoration(
- color: Color(0xFF2ECC71),
- borderRadius: new BorderRadius.circular(5),
- ),
- child: new Center(
- child: new Text(
- 'ENCODE',
- style: new TextStyle(fontSize: 14.0, fontWeight: FontWeight.bold, color: Colors.white),
- ),
- ),
- ),
- ),
- ),
- Expanded(
- child: Container(
- alignment: Alignment(-1.0, -1.0),
- margin: EdgeInsets.all(20.0),
- padding: EdgeInsets.all(4.0),
- decoration: new BoxDecoration(borderRadius: BorderRadius.all(new Radius.circular(5)), color: Color(0xFFF1C40F)),
- child: SingleChildScrollView(reverse: true, child: Text(_encodeOutput))),
- )
- ],
- ),
- ],
- controller: _controller,
- ));
- }
- @override
- void dispose() {
- super.dispose();
- _commandController.dispose();
- }
- }
|