This commit is contained in:
xiangbing 2025-02-10 22:23:21 +08:00
parent 123add14eb
commit 5016d0ca69
2 changed files with 24 additions and 44 deletions

View File

@ -65,5 +65,6 @@ namespace VideoConcat.Common.Tools
} }
return destinationPath; return destinationPath;
} }
} }
} }

View File

@ -134,38 +134,8 @@ namespace VideoConcat.ViewModels
tempList.RemoveAt(index); tempList.RemoveAt(index);
} }
SemaphoreSlim semaphore = new(10); // Limit to 3 threads
List<Task> _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<Task> taskList = []; List<Task> taskList = [];
semaphore = new(10); SemaphoreSlim semaphore = new(10); // Limit to 3 threads
foreach (List<string> combination in result) foreach (List<string> combination in result)
{ {
@ -174,28 +144,37 @@ namespace VideoConcat.ViewModels
{ {
try 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 _tempFileName = $"{DateTime.Now:yyyyMMddHHmmss}{random.Next(100000, 999999)}.mp4";
string _outPutName = Path.Combine($"{VideoModel.FolderPath}", "output", _tempFileName); ; 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; bool _isSuccess = false;
try try
{ {
_isSuccess = FFMpegArguments _isSuccess = FFMpeg.Join(_outPutName, [.. combination]);
.FromConcatInput(temporaryVideoParts)
.OutputToFile(_outPutName, true, options => options
.CopyChannel()
.WithBitStreamFilter(Channel.Audio, Filter.Aac_AdtstoAsc))
.ProcessSynchronously();
} }
catch (Exception ex) catch (Exception ex)
{ {