英文:
How to use ffmpeg_kit_flutter to merge two videos in Flutter
问题
我想将位于我的项目资产文件夹中的intro.mp4视频与cameraRecord输出视频(假设为output.mp4)合并。
我尝试了以下代码,但结果不好(输出视频是并排的而不是顺序的),而且似乎无法使用资产文件夹,我认为所有路径应该来自设备而不是项目(资产...)。
Future<void> mergeRecordWithIntro(String outputPath, String videoPath) async {
emit(MergeVideoLoading());
const String introPath = 'assets/logo.mp4';
const filter = "[0:v]scale=480:640,setsar=1[l];[1:v]scale=480:640,setsar=1[r];[l][r]hstack;[0][1]amix -vsync 0 ";
await FFmpegKit.execute('-y -i $videoPath -i $introPath -filter_complex $filter $outputPath')
.then((value) async {
final returnCode = await value.getReturnCode();
if (ReturnCode.isSuccess(returnCode)) {
GallerySaver.saveVideo(outputPath);
emit(MergeVideoSuccess());
} else if (ReturnCode.isCancel(returnCode)) {
emit(MergeVideoError());
}
});
}
这是我的更新日志屏幕截图,使用以下命令:
-y -i $firstVideoPath -i $secondVideoPath -filter_complex "[0:v]scale=480:640[v0];[1:v]scale=480:640[v1];[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" $outputPath
英文:
i want to merge an intro.mp4 (located in the assets folder of my project) video with the cameraRecord Output video (let's say output.mp4)
i tried this (code below) but it gives a bad result (the output video is side by side not sequential ) also it didn't work with the assets folder i think all the paths should come from the device and not from the project (assets ...)
Future<void> mergeRecordWithIntro(String outputPath, String videoPath) async {
emit(MergeVideoLoading());
const String introPath = 'assets/logo.mp4';
const filter =
" [0:v]scale=480:640,setsar=1[l];[1:v]scale=480:640,setsar=1[r];[l][r]hstack;[0][1]amix -vsync 0 ";
await FFmpegKit.execute(
'-y -i $videoPath -i $videoPath -filter_complex $filter $outputPath')
.then((value) async {
final returnCode = await value.getReturnCode();
if (ReturnCode.isSuccess(returnCode)) {
GallerySaver.saveVideo(outputPath);
emit(MergeVideoSucces());
} else if (ReturnCode.isCancel(returnCode)) {
emit(MergeVideoError());
}
});
this is my updates log screen shot using this command :
-y -i $firstVideoPath -i $secondVideoPath -filter_complex "[0:v]scale=480:640[v0];[1:v]scale=480:640[v1];[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" $outputPath
答案1
得分: 0
首先,将视频保存到项目的资产文件夹中,然后将其移到临时文件夹并使用该路径。
final byteData = await rootBundle.load('assets/logo.mp4');
final file = await File('${Directory.systemTemp.path}${path.replaceAll('assets', '')}').create();
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
final firstVideoPath = file.path;
接下来,合并视频的命令是 concat
,请修复它。*如果您使用不同的分辨率或编解码器,可能需要进行调整。
await FFmpegKit.execute('-y -i "concat:$firstVideoPath|$secondVideoPath" -c copy $outputPath')
最后,使用 gal package 而不是 gallery_saver,后者已经两年未维护。
await Gal.putVideo($outputPath);
英文:
First, save the video in the project's assets folder to a temporary folder and use the path there.
final byteData = await rootBundle.load('assets/logo.mp4');
final file = await File('${Directory.systemTemp.path}${path.replaceAll('assets', '')}').create();
await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
final firstVideoPath = file.path;
Next, the command to merge videos is concat
, so fix it. *If you have a different resolution or codec, you may need to fix it.
await FFmpegKit.execute('-y -i "concat:$firstVideoPath|$secondVideoPath" -c copy $outputPath')
Finally, use the gal package instead of the gallery_saver, which is already two years out of maintenance.
await Gal.putVideo($outputPath);
答案2
得分: 0
这个命令对我有效,唯一的问题是输出视频质量非常低,所以...!! :
-y -i $firstVideoPath -i $videoPath -r 24000/1001 -filter_complex "[0:v]scale=1080:1920[v0];[1:v]scale=1080:1920[v1];[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" -vsync 2 $outputPath
英文:
this command worked for me the only problem is the output video quality is very low so... !! :
-y -i $firstVideoPath -i $videoPath -r 24000/1001 -filter_complex "[0:v]scale=1080:1920[v0];[1:v]scale=1080:1920[v1];[v0][0:a][v1][1:a]concat=n=2:v =1:a=1[outv][outa]" -map "[outv]" -map "[outa]" -vsync 2 $outputPath
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论