using FFmpeg.NET.Services;
using MapsterMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Diagnostics;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using UserCenter.Model;
using UserCenter.Model.Enum;
using VideoAnalysisCore.AICore.GPT.Dto;
using VideoAnalysisCore.AICore.SherpaOnnx;
using VideoAnalysisCore.Common;
using VideoAnalysisCore.Common.Expand;
using VideoAnalysisCore.Controllers.Dto;
using VideoAnalysisCore.Model;
using VideoAnalysisCore.Model.Dto;
using VideoAnalysisCore.Model.Enum;
using Yitter.IdGenerator;
namespace VideoAnalysisCore.Controllers
{
///
/// 路由菜单
///
public class VideoTaskController : BackController
{
readonly Repository baseService;
readonly Repository videoQuestionDB;
readonly Repository videoKonwPointDB;
readonly Repository videoTaskStageDB;
readonly Repository videoQuestionKonwDB;
readonly Repository taskLogDB;
readonly RedisManager redisManager;
public readonly SenseVoice senseVoice;
public readonly FunASRNano funASRNano;
private readonly IMapper mp;
public VideoTaskController(Repository baseService, RedisManager redisManager,
Repository videoQuestionDB,
Repository videoQuestionKonwDB, Repository videoKonwPointDB, SenseVoice senseVoice, IMapper mp, Repository taskLogDB, FunASRNano funASRNano, Repository videoTaskStageDB) : base(baseService)
{
this.baseService = baseService;
this.redisManager = redisManager;
this.videoQuestionDB = videoQuestionDB;
this.videoQuestionKonwDB = videoQuestionKonwDB;
this.videoKonwPointDB = videoKonwPointDB;
this.senseVoice = senseVoice;
this.mp = mp;
this.taskLogDB = taskLogDB;
this.funASRNano = funASRNano;
this.videoTaskStageDB = videoTaskStageDB;
}
private string GetClientIpAddress()
{
// 检查 X-Forwarded-For 请求头
if (HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")
&& !string.IsNullOrEmpty(HttpContext.Request.Headers["X-Forwarded-For"]))
return HttpContext.Request.Headers["X-Forwarded-For"].ToString();
if (HttpContext.Connection.RemoteIpAddress != null)
return HttpContext.Connection.RemoteIpAddress.ToString();
throw new Exception("未能获取到客户端ip地址");
}
#if DEBUG
///
/// 初始化主库表
///
/// 文件流
///
[HttpGet(Name = "InitDbTable")]
public IActionResult InitDbTable()
{
var b = AppCommon.Config.DB.UpdateTable;
AppCommon.Config.DB.UpdateTable = true;
SqlSugarExpand.InitDbTable();
AppCommon.Config.DB.UpdateTable = b;
return Ok();
}
#endif
///
/// 插入批量任务id
///
/// 是否执行任务
///
[HttpPost(Name = "JoinQueue")]
public IActionResult JoinQueue(long[] ids)
{
if (ids == null || ids.Count() == 0)
return BadRequest("录入数据无效");
redisManager.JoinQueue(ids);
return Ok();
}
///
/// 当前消费者 继续执行任务
///
/// 是否执行任务
///
[HttpGet(Name = "StartTask")]
public IActionResult StartTask(bool task)
{
if (task)
redisManager.RestartTask();
else
redisManager.StopTaskAsync();
return Ok();
}
///
/// 语音识别
///
/// 文件流
///
[HttpGet(Name = "AudioRecognitionUrl")]
public async Task AudioRecognitionUrl(string url)
{
try
{
using HttpClient client = new HttpClient();
// 发送GET请求获取网络文件流
using var networkStream = await client.GetStreamAsync(url);
var res = senseVoice.RunTask(networkStream);
return Ok(res);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
///
/// 语音识别
///
/// 文件流
///
[HttpPost(Name = "AudioRecognition")]
public IActionResult AudioRecognition(IFormFile file)
{
using var s = file.OpenReadStream();
var res = senseVoice.RunTask(s);
return Ok(res);
}
///
/// 语音识别
///
/// 文件流
///
[HttpPost(Name = "AudioRecognition_test")]
public IActionResult AudioRecognition_test(IFormFile file)
{
using var s = file.OpenReadStream();
var x = AppCommon.Services.GetService();
x.Init();
senseVoice.RunTask(s);
for (int i = 0; i < SenseVoice.cachedValue.Count(); i++)
{
Console.WriteLine($"字幕索引=>{i}");
Console.WriteLine($"ssv=>{SenseVoice.cachedValue[i].z1}");
Console.WriteLine($"fun=>{SenseVoice.cachedValue[i].z2}");
Console.WriteLine();
}
return Ok();
}
///
/// 获取FTS_Data str
///
/// 路径
///
[HttpGet(Name = "fts_data")]
public async Task FTS_Data(string path = "itn_subject_sx.fst")
{
var hotwords = JsonSerializer
.Deserialize(System.IO.File.ReadAllText(Path.Combine(AppCommon.AIModelFile, "Hotwords.json")));
var res = new List(100);
foreach (var element in hotwords.OrderByDescending(s => s.key.Count()))
foreach (var e in element.v)
res.Add($"""("{e}", "{element.key}")""");
var pyFile = System.IO.File.ReadAllText(Path.Combine(AppCommon.AIModelFile, "sherpa-onnx-fst.py"));
var resStr = pyFile
.Replace("(fts_data)", "[" + string.Join(',', res) + "]")
.Replace("(path)", path);
return Ok(resStr);
}
///
/// 重新开始执行GPT分析taskId/tagId二选一
///
///
/// 自定义id
/// 切换任务所属学科 null忽略
///
[HttpGet]
public async Task ReStartTask(long taskId, string? tagId, SubjectEnum? subject)
{
var task = await baseService.AsQueryable()
.WhereIF(taskId != 0, s => s.Id == taskId)
.WhereIF(!string.IsNullOrEmpty(tagId), s => s.TagId == tagId)
.FirstAsync();
if (task is null)
return BadRequest("未能找到对应任务");
if (subject is not null)
{
task.Subject = subject;
await baseService.UpdateAsync(task);
}
//todo重新开始执行GPT分析
return BadRequest("任务未实现");
return Ok();
}
///
/// 视频处理[批量]
///
/// 请求体
///
[HttpPost(Name = "VideoAnalysis_Batch")]
public async Task VideoAnalysis_Batch(VideoAnalysisReq[] req)
{
foreach (var item in req)
await VideoAnalysis(item);
return Ok();
}
///
/// 视频处理
///
/// 请求体
///
[HttpPost(Name = "VideoAnalysis")]
public async Task VideoAnalysis(VideoAnalysisReq req)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
if (await baseService.IsAnyAsync(s => s.TagId == req.TagId))
return BadRequest("重复添加");
// 自动映射属性到哈希
var task = new VideoTask()
{
Id = YitIdHelper.NextId(),
ComeFrom = GetClientIpAddress(),
MediaUrl = req.MediaUrl,
ApiToken = req.ApiToken,
CourseId = req.CourseId,
Subject = req.Subject,
Tag = req.Tag,
TagId = req.TagId,
PPTVideoCode = req.PPTVideoCode,
PPTVideoUrl = req.PPTVideoUrl,
VideoType = req.VideoType
};
//入库
await baseService.InsertAsync(task);
redisManager.Redis.LPush(RedisExpandKey.ChannelKey, task.Id);
return Ok(task.Id);
}
public override async Task PageList([FromBody] QueryRequestBase model)
{
var sqlquery = base.BaseQuery(model)
.Select(s => new VideoTask
{
Id = s.Id,
TagId = s.TagId,
VideoType = s.VideoType,
LastEnum = s.LastEnum,
Subject = s.Subject,
ComeFrom = s.ComeFrom,
MediaUrl = s.MediaUrl,
CreateTime = s.CreateTime,
ErrorMessage = s.ErrorMessage,
});
RefAsync total = 0;
var data = await sqlquery.ToPageListAsync(model.PageIndex + 1, model.PageSize, total);
return new PageResult() { Data = data, Total = total };
}
public override Task Edit([FromBody] VideoTask model) => throw new NotImplementedException();
public override Task Del([FromBody] params long[] ids) => throw new NotImplementedException();
///
/// 重试任务
///
/// 任务id
/// 任务类型
///
[HttpGet]
public async Task ReStart(long id, RedisChannelEnum selectEnum)
{
await redisManager.AddTaskLog(id,"手动重试任务");
await redisManager.ClearTaskError(id);
_ = Task.Run(async () =>
await redisManager.InsertChannel(selectEnum, id)
);
}
///
/// 刷新数据
///
/// 任务id
///
[HttpGet]
public async Task RowRload(long id)
{
if (id == 0)
return BadRequest("无效id");
var d = await redisManager.Redis.HMGetAsync(RedisExpandKey.Task(id),
"Progress", "LastEnum", "StartTime", "ErrorMessage");
var logArr = await taskLogDB.AsQueryable()
.Where(s => s.VideoTaskId == id)
.ToArrayAsync();
var insertData = (await redisManager.Redis
.LRangeAsync(RedisExpandKey.TaskLog, 0, 99))
.Where(s => s.VideoTaskId == id);
logArr = logArr.Concat(insertData).ToArray();
return Ok(new
{
Progress = d[0],
LastEnum = d[1]?.ToEnum().ToString(),
StartTime = d[2] != null
? JsonSerializer.Deserialize>(d[2])
: null,
ErrorMessage = d[3],
Logs = logArr,
});
}
///
/// 预览任务结果
///
/// 任务id
///
[HttpGet]
public async Task