using FFmpeg.NET.Events; using FFmpeg.NET; using VideoAnalysisCore.AICore.SherpaOnnx; using VideoAnalysisCore.Common; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; using System.Xml.Linq; using System.Runtime.InteropServices; using SqlSugar.IOC; using VideoAnalysisCore.Model; namespace VideoAnalysisCore.AICore.FFMPGE { /// /// Ffmpeg处理程序 /// public class FFMPGEHandle { /// /// /// public static string FFmpegPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? $"/usr/bin/ffmpeg" : Path.Combine(AppCommon.AIModelFile, "ffmpeg.exe"); public static string Task = string.Empty; /// /// 音频转码为 wav_16k /// /// 任务id /// public static async Task Audio2WAV16KAsync(string task) { Task = task; var filePath = await DbScoped.SugarScope .Queryable() .Where(s => s.Id == long.Parse(task)) .Select(s=>s.LocalMediaPath).FirstAsync(); if (string.IsNullOrEmpty(filePath)) throw new Exception($"任务id[{task}] 无效"); // 打开输入文件 var inputFile = new InputFile(filePath); var outputFile = new OutputFile(Path.Combine(task.LocalPath(), Path.GetFileNameWithoutExtension(filePath) + ".wav")); var ffmpeg = new Engine(FFmpegPath); //ffmpeg.Progress += OnProgress; //ffmpeg.Data += OnData; ffmpeg.Complete += OnComplete; ffmpeg.Error += (sender, e) => { var ee = new Exception($"音频转码出现异常 \r\n[{e.Input.Name} => {e.Output.Name}]: 错误: {e.Exception.Message}"); RedisExpand.SetTaskErrorMessage(long.Parse(task), ee); throw ee; }; var conversionOptions = new ConversionOptions { ExtraArguments = "-ar 16000 -ac 1" //+ (AppCommon.AppSetting.FFmpeg.TimeSlice == 0 //?string.Empty //: $"-f segment -reset_timestamps 1 -segment_time {AppCommon.AppSetting.FFmpeg.TimeSlice}") }; var res = await ffmpeg.ConvertAsync(inputFile, outputFile, conversionOptions); } private static void OnProgress(object sender, ConversionProgressEventArgs e) { Console.WriteLine("[{0} => {1}]", e.Input.MetaData.FileInfo.Name, e.Output.Name); Console.WriteLine("比特率: {0}", e.Bitrate); Console.WriteLine("Fps: {0}", e.Fps); Console.WriteLine("基本框架: {0}", e.Frame); Console.WriteLine("处理持续时间: {0}", e.ProcessedDuration); Console.WriteLine("Size: {0} kb", e.SizeKb); Console.WriteLine("总持续时间: {0}\n", e.TotalDuration); } private static void OnData(object sender, ConversionDataEventArgs e) { Console.WriteLine(e.Data); } private static void OnComplete(object sender, ConversionCompleteEventArgs e) { Console.WriteLine("转换完成=>" + e.Output.Name); //加入下一队列 RedisExpand.InsertChannel(Enum.RedisChannelEnum.ParsingCaptions, Task); } } }