From 5016d0ca6946ad177394ccac841a1fd3e8d0fba9 Mon Sep 17 00:00:00 2001 From: xiangbing Date: Mon, 10 Feb 2025 22:23:21 +0800 Subject: [PATCH] update --- Common/Tools/VideoCombine.cs | 1 + ViewModels/VideoViewModel.cs | 67 +++++++++++++----------------------- 2 files changed, 24 insertions(+), 44 deletions(-) diff --git a/Common/Tools/VideoCombine.cs b/Common/Tools/VideoCombine.cs index d857e20..71ec145 100644 --- a/Common/Tools/VideoCombine.cs +++ b/Common/Tools/VideoCombine.cs @@ -65,5 +65,6 @@ namespace VideoConcat.Common.Tools } return destinationPath; } + } } diff --git a/ViewModels/VideoViewModel.cs b/ViewModels/VideoViewModel.cs index 07734f1..6c4704d 100644 --- a/ViewModels/VideoViewModel.cs +++ b/ViewModels/VideoViewModel.cs @@ -134,38 +134,8 @@ namespace VideoConcat.ViewModels tempList.RemoveAt(index); } - - SemaphoreSlim semaphore = new(10); // Limit to 3 threads - - List _tasks = []; - - foreach (var _path in _converVideoPath) - { - await semaphore.WaitAsync(); // Wait when more than 3 threads are running - var _task = Task.Run(() => - { - try - { - _clearPath.Add(VideoCombine.ConvertVideos(_path)); - } - finally - { - semaphore.Release(); // Work is done, signal to semaphore that more work is possible - } - }); - _tasks.Add(_task); - } - - await Task.WhenAll(_tasks).ContinueWith((task) => - { - LogUtils.Info($"转换完成,用时{(DateTime.Now - startTime).TotalSeconds}秒"); - }); - - LogUtils.Info("开始拼接视频"); - - List taskList = []; - semaphore = new(10); + SemaphoreSlim semaphore = new(10); // Limit to 3 threads foreach (List combination in result) { @@ -174,28 +144,37 @@ namespace VideoConcat.ViewModels { try { + combination.ForEach(video => + { + IMediaAnalysis videoInfo = FFProbe.Analyse(video); + if(videoInfo.PrimaryVideoStream.CodecName == "hevc") + { + // 使用 FFmpegCore 进行视频重新编码 + bool result = FFMpegArguments + .FromFileInput(video) + .OutputToFile("2222.mp4", true, options => options + .WithVideoCodec(VideoCodec.LibX264) // 使用 libx264 编码器进行视频编码 + .WithConstantRateFactor(23) // 设置视频质量,数值越小质量越高,默认值是 23 + .WithSpeedPreset(Speed.Medium) + .WithAudioCodec(AudioCodec.Aac) + // .WithPreset(VideoPreset.Medium) // 设置编码速度和压缩比的预设,Medium 是一个平衡的选择 + // .WithAudioCodec(AudioCodec.Copy) // 直接复制音频流,不进行重新编码 + ) + .ProcessSynchronously(); + } + }); + string _tempFileName = $"{DateTime.Now:yyyyMMddHHmmss}{random.Next(100000, 999999)}.mp4"; string _outPutName = Path.Combine($"{VideoModel.FolderPath}", "output", _tempFileName); ; - var temporaryVideoParts = combination.Select((_videoPath) => - { - string _tempPath = Path.GetDirectoryName(_videoPath) ?? ""; - //GlobalFFOptions.Current.TemporaryFilesFolder - return Path.Combine(_tempPath, $"{Path.GetFileNameWithoutExtension(_videoPath)}{FileExtension.Ts}"); - }).ToArray(); - + bool _isSuccess = false; try { - _isSuccess = FFMpegArguments - .FromConcatInput(temporaryVideoParts) - .OutputToFile(_outPutName, true, options => options - .CopyChannel() - .WithBitStreamFilter(Channel.Audio, Filter.Aac_AdtstoAsc)) - .ProcessSynchronously(); + _isSuccess = FFMpeg.Join(_outPutName, [.. combination]); } catch (Exception ex) {