Learn.VideoAnalysis/VideoAnalysisCore/AICore/FFMPGE/FFMPGEHandle.cs

79 lines
2.9 KiB
C#

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;
namespace VideoAnalysisCore.AICore.FFMPGE
{
/// <summary>
/// Ffmpeg处理程序
/// </summary>
public class FFMPGEHandle
{
/// <summary>
///
/// </summary>
public static string FFmpegPath = Path.Combine(AppCommon.AIModelFile, "ffmpeg.exe");
/// <summary>
/// 音频转码为 wav_16k
/// </summary>
/// <param name="task">任务id</param>
/// <returns></returns>
public static async Task Audio2WAV16KAsync(string task)
{
var filePath = RedisExpand.Redis.HGet(RedisExpandKey.Task(task), "LocalMediaPath");
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) =>
{
throw new Exception($"[{e.Input.Name} => {e.Output.Name}]: 错误: {e.Exception.Message}");
};
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);
//加入下一队列
RedisExpand.InsertChannel(Enum.RedisChannelEnum.ParsingCaptions, task);
}
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);
}
}
}