1+ using MaiChartManager . Utils ;
2+ using Microsoft . AspNetCore . Mvc ;
3+ using Xabe . FFmpeg ;
4+
5+ namespace MaiChartManager . Controllers ;
6+
7+ [ ApiController ]
8+ [ Route ( "MaiChartManagerServlet/[action]Api" ) ]
9+ public class VrcProcessController ( StaticSettings settings , ILogger < VrcProcessController > logger ) : ControllerBase
10+ {
11+ [ HttpPost ]
12+ public void GenAllMusicPreviewMp3ForVrc ( [ FromForm ] string targetDir , [ FromForm ] int maxConcurrency )
13+ {
14+ Task . Run ( async ( ) =>
15+ {
16+ // using var semaphore = new SemaphoreSlim(maxConcurrency);
17+ // var tasks = new List<Task>();
18+ var allAcb = StaticSettings . AcbAwb . Where ( x => x . Key . StartsWith ( "music" ) && x . Key . EndsWith ( ".acb" ) ) . ToDictionary ( ) ;
19+ foreach ( var key in allAcb . Keys )
20+ {
21+ // await semaphore.WaitAsync();
22+ // tasks.Add(Task.Run(async () =>
23+ // {
24+ try
25+ {
26+ var musicId = int . Parse ( key [ 5 ..^ 4 ] ) ;
27+ var previewTime = CriUtils . GetAudioPreviewTime ( allAcb [ key ] ) ;
28+ var wav = await AudioConvert . GetCachedWavPath ( musicId ) ;
29+
30+ if ( wav is null )
31+ {
32+ logger . LogWarning ( "音频文件不存在 {musicId}" , musicId ) ;
33+ continue ;
34+ }
35+ if ( previewTime . EndTime < previewTime . StartTime )
36+ {
37+ logger . LogWarning ( "previewTime.EndTime < previewTime.StartTime {musicId} {endTime} {startTime}" , musicId , previewTime . EndTime , previewTime . StartTime ) ;
38+ }
39+ var mp3Path = Path . Combine ( targetDir , $ "{ musicId } .mp3") ;
40+ // logger.LogInformation("转换中 {musicId}", musicId);
41+ var conversion = await FFmpeg . Conversions . FromSnippet
42+ . Split ( wav , mp3Path , TimeSpan . FromSeconds ( previewTime . StartTime ) , TimeSpan . FromSeconds ( previewTime . EndTime - previewTime . StartTime ) ) ;
43+
44+ await conversion . SetOutputFormat ( Format . mp3 )
45+ . Start ( ) ;
46+ }
47+ catch ( Exception ex )
48+ {
49+ logger . LogError ( ex , "处理音频文件出错 {key}" , key ) ;
50+ }
51+ // finally
52+ // {
53+ // semaphore.Release(); // 释放信号量
54+ // }
55+ // }));
56+ }
57+
58+ // await Task.WhenAll(tasks);
59+ } ) ;
60+ }
61+ }
0 commit comments