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

New onError param in export functions #98

Merged
merged 1 commit into from
Jun 23, 2022
Merged
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
67 changes: 31 additions & 36 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,36 +137,34 @@ class _VideoEditorState extends State<VideoEditor> {
// preset: VideoExportPreset.medium,
// customInstruction: "-crf 17",
onProgress: (stats, value) => _exportingProgress.value = value,
onError: (e, s) => _exportText = "Error on export video :(",
onCompleted: (file) {
_isExporting.value = false;
if (!mounted) return;
if (file != null) {
final VideoPlayerController videoController =
VideoPlayerController.file(file);
videoController.initialize().then((value) async {
setState(() {});
videoController.play();
videoController.setLooping(true);
await showDialog(
context: context,
builder: (_) => Padding(
padding: const EdgeInsets.all(30),
child: Center(
child: AspectRatio(
aspectRatio: videoController.value.aspectRatio,
child: VideoPlayer(videoController),
),

final VideoPlayerController videoController =
VideoPlayerController.file(file);
videoController.initialize().then((value) async {
setState(() {});
videoController.play();
videoController.setLooping(true);
await showDialog(
context: context,
builder: (_) => Padding(
padding: const EdgeInsets.all(30),
child: Center(
child: AspectRatio(
aspectRatio: videoController.value.aspectRatio,
child: VideoPlayer(videoController),
),
),
);
await videoController.pause();
videoController.dispose();
});
_exportText = "Video success export!";
} else {
_exportText = "Error on export video :(";
}
),
);
await videoController.pause();
videoController.dispose();
});

_exportText = "Video success export!";
setState(() => _exported = true);
Future.delayed(const Duration(seconds: 2),
() => setState(() => _exported = false));
Expand All @@ -177,21 +175,18 @@ class _VideoEditorState extends State<VideoEditor> {
void _exportCover() async {
setState(() => _exported = false);
await _controller.extractCover(
onError: (e, s) => _exportText = "Error on cover exportation :(",
onCompleted: (cover) {
if (!mounted) return;

if (cover != null) {
_exportText = "Cover exported! ${cover.path}";
showDialog(
context: context,
builder: (_) => Padding(
padding: const EdgeInsets.all(30),
child: Center(child: Image.memory(cover.readAsBytesSync())),
),
);
} else {
_exportText = "Error on cover exportation :(";
}
_exportText = "Cover exported! ${cover.path}";
showDialog(
context: context,
builder: (_) => Padding(
padding: const EdgeInsets.all(30),
child: Center(child: Image.memory(cover.readAsBytesSync())),
),
);

setState(() => _exported = true);
Future.delayed(const Duration(seconds: 2),
Expand Down
63 changes: 43 additions & 20 deletions lib/domain/bloc/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class VideoEditorController extends ChangeNotifier {
_updateTrimRange();
}

generateDefaultCoverThumnail();
generateDefaultCoverThumbnail();

notifyListeners();
}
Expand Down Expand Up @@ -341,7 +341,7 @@ class VideoEditorController extends ChangeNotifier {
}

/// Generate cover at [startTrim] time in milliseconds
void generateDefaultCoverThumnail() async {
void generateDefaultCoverThumbnail() async {
final defaultCover =
await generateCoverThumbnail(timeMs: startTrim.inMilliseconds);
updateSelectedCover(defaultCover);
Expand Down Expand Up @@ -419,7 +419,9 @@ class VideoEditorController extends ChangeNotifier {

/// Export the video using this edition parameters and return a `File`.
///
/// The [onCompleted] param must be set to retun the exported [File] video
/// The [onCompleted] param must be set to return the exported [File] video.
///
/// The [onError] function provides the [Exception] and [StackTrace] that causes the exportation error.
///
/// If the [name] is `null`, then it uses this video filename.
///
Expand All @@ -442,7 +444,8 @@ class VideoEditorController extends ChangeNotifier {
///
/// Set [isFiltersEnabled] to `false` if you do not want to apply any changes
Future<void> exportVideo({
required void Function(File? file) onCompleted,
required void Function(File file) onCompleted,
void Function(Object, StackTrace)? onError,
String? name,
String? outDir,
String format = "mp4",
Expand Down Expand Up @@ -487,12 +490,19 @@ class VideoEditorController extends ChangeNotifier {
final state =
FFmpegKitConfig.sessionStateToString(await session.getState());
final code = await session.getReturnCode();
final failStackTrace = await session.getFailStackTrace();

debugPrint(
"FFmpeg process exited with state $state and return code $code.${(failStackTrace == null) ? "" : "\\n$failStackTrace"}");

onCompleted(code?.isValueSuccess() == true ? File(outputPath) : null);
if (code?.isValueSuccess() == true) {
onCompleted(File(outputPath));
} else {
if (onError != null) {
onError(
Exception(
'FFmpeg process exited with state $state and return code $code.\n${await session.getOutput()}'),
StackTrace.current,
);
}
return;
}
},
null,
onProgress != null
Expand Down Expand Up @@ -569,7 +579,9 @@ class VideoEditorController extends ChangeNotifier {

/// Export this selected cover, or by default the first one, return an image [File].
///
/// The [onCompleted] param must be set to retun the exported [File] cover
/// The [onCompleted] param must be set to return the exported [File] cover
///
/// The [onError] function provides the [Exception] and [StackTrace] that causes the exportation error.
///
/// If the [name] is `null`, then it uses this video filename.
///
Expand All @@ -587,7 +599,8 @@ class VideoEditorController extends ChangeNotifier {
///
/// Set [isFiltersEnabled] to `false` if you do not want to apply any changes
Future<void> extractCover({
required void Function(File? file) onCompleted,
required void Function(File file) onCompleted,
void Function(Object, StackTrace)? onError,
String? name,
String? outDir,
String format = "jpg",
Expand All @@ -598,11 +611,14 @@ class VideoEditorController extends ChangeNotifier {
}) async {
final String tempPath = outDir ?? (await getTemporaryDirectory()).path;
// file generated from the thumbnail library or video source
final String? coverPath = await _generateCoverFile(
quality: quality,
);
final String? coverPath = await _generateCoverFile(quality: quality);
if (coverPath == null) {
debugPrint("ERROR ON COVER EXTRACTION WITH VideoThumbnail LIBRARY");
if (onError != null) {
onError(
Exception('VideoThumbnail library error while exporting the cover'),
StackTrace.current,
);
}
return;
}
name ??= path.basenameWithoutExtension(file.path);
Expand Down Expand Up @@ -633,12 +649,19 @@ class VideoEditorController extends ChangeNotifier {
final state =
FFmpegKitConfig.sessionStateToString(await session.getState());
final code = await session.getReturnCode();
final failStackTrace = await session.getFailStackTrace();

debugPrint(
"FFmpeg process exited with state $state and return code $code.${(failStackTrace == null) ? "" : "\\n$failStackTrace"}");

onCompleted(code?.isValueSuccess() == true ? File(outputPath) : null);
if (code?.isValueSuccess() == true) {
onCompleted(File(outputPath));
} else {
if (onError != null) {
onError(
Exception(
'FFmpeg process exited with state $state and return code $code.\n${await session.getOutput()}'),
StackTrace.current,
);
}
return;
}
},
null,
onProgress,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/cover/cover_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class _CoverViewerState extends State<CoverViewer> {

void _checkIfCoverIsNull() {
if (widget.controller.selectedCoverVal!.thumbData == null) {
widget.controller.generateDefaultCoverThumnail();
widget.controller.generateDefaultCoverThumbnail();
}
}

Expand Down