冒险解谜游戏中文网 ChinaAVG

标题: 【汉化工具系列 #1】指定wave格式转换工具(cvtWave) [打印本页]

作者: shane007    时间: 2023-9-4 08:56
标题: 【汉化工具系列 #1】指定wave格式转换工具(cvtWave)

4 `) g$ q& c/ n% m为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的( Y! ^% l/ ?/ _' l: K2 {
sampleRate为16000的格式。
, ?$ e7 Z" R' e本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。8 _& |; D# N& C
执行后,会在目标目录生成和源文件相同目录结构的wave文件,  d- f" h- I2 u6 @0 k
供语音识别之用。
! K- a1 Q% g  C# k8 d8 ~  {: V3 M% x  N1 M' w) l3 W# A
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,$ S. n& Y3 X" x4 A5 l2 `$ o% L
可以从以下软件中获得。
% E- y; @6 h# ]7 n/ ?https://softaro.net/ffmpeggui/% x  S+ i1 u- \

0 a2 A2 W- i# j# o3 B9 m工具代码如下,vs2022编译即可
$ R/ e' X' I8 N6 Z, m2 M) H* p4 R
  1. using System;9 q2 @' h1 P# R0 ^2 M
  2. using System.Diagnostics;9 \+ X  @. S2 l
  3. using System.IO;3 z$ U$ R4 X( X" y6 f
  4. 6 B8 g$ U4 P1 `- C9 o* B! w& S
  5. class Program
    . T) J* [' _2 C
  6. {
    2 d' p& i# h* y
  7.     static void Main(string[] args)
    " z/ |' D% M  b3 z% }9 C0 v
  8.     {
    . Y/ Z9 A5 z- C3 n
  9.         Console.WriteLine("cvtWave");
    # d: Q% ?/ @/ z7 A0 z5 @
  10.         Console.WriteLine("请输入源文件夹路径 (folder1):");- h, T# f' f& ]7 d/ W6 @; {
  11.         string sourceFolder = Console.ReadLine();
    + D6 q7 E$ [) g  E! L7 X0 b1 R
  12.         Console.WriteLine("请输入目标文件夹路径 (folder2):");
    8 Z  g/ `" U; u8 o
  13.         string destinationFolder = Console.ReadLine();
    * l/ F; s; O  L3 g* [
  14. * N; M: E6 }: h$ k, B" m- l
  15.         ConvertFilesToWAV(sourceFolder, destinationFolder);/ U) O: ~7 B# Q: x( ^* E
  16. 3 {9 o4 _' c( z; d4 L* ?
  17.         Console.WriteLine("转换完成.");
    9 R& o1 A  h: j) {. ?5 Q8 v3 s
  18.     }
    8 ^! W0 P6 I8 ?% f5 T5 u

  19. : \5 ?( q( E( d' O. N
  20.     static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
    % b* y3 r6 K2 L% ^8 B+ e
  21.     {* e% o! ]. t0 P2 G
  22.         foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
    7 `& E* E  O( L0 B4 l" O
  23.         {
    1 {& u1 t( Z. ~$ N9 z, ?/ w
  24.             string extension = Path.GetExtension(sourceFilePath).ToLower();
    , A9 v# X$ s: D& J0 M' V) d
  25. - t3 x' g/ g* t" I& t. W" E6 V% J
  26.             if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")* x  B6 z1 _, W1 j+ p6 X  |" h  _
  27.             {
    ) B4 B$ e0 d* w7 @  @
  28.                 string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);! s4 l  |0 i/ @. M. J& T8 E
  29.                 string destinationFilePath = Path.Combine(destinationFolder, relativePath);0 n1 D. s, H4 `. c% p
  30.                 destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");, W' P  v( |0 X

  31. & X8 J9 x; G& s9 N( g) u2 u1 h4 w
  32.                 Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));- X4 G0 H( b" Y+ s- E3 X2 x9 m

  33. 0 p, v7 H; g  h
  34.                 string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";
    . y- Z9 e/ z2 N) h& M" O, u. Z) U

  35. + }' U1 [" m8 v/ G" g& a* y4 i* q
  36.                 Process ffmpegProcess = new Process
    0 H" a: q1 Z. z8 u+ X% b
  37.                 {
    ; f% Z( `; g9 R8 Y2 ?
  38.                     StartInfo = new ProcessStartInfo1 x: P. l6 Z( X/ C) e
  39.                     {
    / G/ M9 `" a& G% n$ U
  40.                         FileName = "ffmpeg64.exe",
    ( q& n1 p8 S, B6 X# X+ ^* B% D
  41.                         Arguments = ffmpegArgs,, y. E8 l6 \0 M: ^6 G7 {1 f
  42.                         UseShellExecute = false,: r  q5 {9 d5 k$ T
  43.                         RedirectStandardError = true," K7 a2 [! v9 B; \- u
  44.                         CreateNoWindow = true7 ~" W& c6 A1 g  g
  45.                     }% `( Z  n7 Z0 X7 K1 }; `3 r! B' h- d
  46.                 };
    & d5 w/ A4 C; }, c/ E
  47. " i% F4 T# S; M2 `5 k
  48.                 ffmpegProcess.Start();% j4 b7 E' c) h4 v
  49. & w2 H$ i$ b. Q0 P! \; ^9 `+ {: i
  50.                 while (!ffmpegProcess.StandardError.EndOfStream)5 I- @# b# r2 g! m' P% b
  51.                 {
    9 w# M4 V! \2 U" W% _1 I3 U
  52.                     string line = ffmpegProcess.StandardError.ReadLine();5 V/ z, L$ ~* l6 g  H( d! g5 Q! H
  53.                     Console.WriteLine(line);
    9 f& o6 f) I/ A% c' v
  54.                 }
    , C* z* `8 }% v7 d' B
  55. $ G  u, W7 M8 w. B3 s# B
  56.                 ffmpegProcess.WaitForExit();* w- g% H- ~1 R  M2 m) h. _
  57.             }
    7 ^2 R. X  d+ A9 m6 o- ?: e
  58.         }" N5 s( o3 R* i6 U$ Z6 H" C
  59.     }$ |/ e5 O% n8 K0 {  U7 _9 }% O
  60. }6 w+ @' H) B/ |! X
复制代码
- d% ]# X# r0 l
/ ^! i: `# n" E; O5 S( A

* `1 Y- z" \1 {9 W/ \+ F6 p
5 F5 a6 q& b" S3 x; ]" v




欢迎光临 冒险解谜游戏中文网 ChinaAVG (https://chinaavg.com/) Powered by Discuz! X3.2