冒险解谜游戏中文网 ChinaAVG

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

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

* ~, O6 h3 t' B& f* @( ]5 x为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的
4 e' m9 A4 H' KsampleRate为16000的格式。, \- O" f2 \* I% u1 u6 X( x: t
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。& ]2 Q) ?  T1 i1 u
执行后,会在目标目录生成和源文件相同目录结构的wave文件,+ g$ B+ V' W6 Y( j1 h  I$ i( ]- y
供语音识别之用。
* |; l$ J$ C2 ]
9 b! j; |* ~- r8 h* @/ }9 A本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,
9 j$ U+ i( l, |, r可以从以下软件中获得。: I3 b" z% |7 [2 ]3 g" T
https://softaro.net/ffmpeggui/1 z% X. @: N. y8 |2 X& W2 y

6 J3 G0 F* Z0 _4 ]工具代码如下,vs2022编译即可# D8 a7 a* W: E( G0 n% D& h, o
  1. using System;
    - D7 I* L' l9 v  l1 q% x+ s
  2. using System.Diagnostics;7 [6 R  V- K. d' k& P! ]$ L
  3. using System.IO;
    # k& {) L4 L) r. B; l
  4. " H5 e" x1 T* H$ n9 N+ S
  5. class Program
    ; r# Q5 x) `3 o
  6. {
    ; y0 v: i0 }  M- i7 i
  7.     static void Main(string[] args)
    # H5 m# P# W/ U) M' n( h
  8.     {. {- \6 e% ]! ~2 i
  9.         Console.WriteLine("cvtWave");& o/ ?4 Z% t6 D; R& H  ~" A2 }( S
  10.         Console.WriteLine("请输入源文件夹路径 (folder1):");, h' K1 a, y5 V! B" T  f
  11.         string sourceFolder = Console.ReadLine();% i/ }6 w3 v+ A5 ~' _% f. ~; E9 w
  12.         Console.WriteLine("请输入目标文件夹路径 (folder2):");
    : y/ r) w  V4 L3 v
  13.         string destinationFolder = Console.ReadLine();
    ) n& S) v0 ~! a! R
  14. 3 Z+ E  d4 m! b" C
  15.         ConvertFilesToWAV(sourceFolder, destinationFolder);
    . e& E0 h7 U) m: B0 R4 q) ]4 y! R
  16.   Y+ R# `$ G4 ?, U' |
  17.         Console.WriteLine("转换完成.");, `: q3 V" j4 Y: E, V
  18.     }7 t2 W! w$ V+ r6 U/ t3 E8 Y# V
  19. " D! ~. ?2 C2 f/ U! h+ ]
  20.     static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)7 R7 M* u( B" _0 r, W- M: @7 l0 f
  21.     {
    + l: o1 q+ u8 _2 v$ Q
  22.         foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))/ A3 R9 U+ a0 c8 n0 s5 R
  23.         {8 Y: W& x! `' y7 @$ J% U* z0 f
  24.             string extension = Path.GetExtension(sourceFilePath).ToLower();7 ~! Z- D# m' l6 h) N# H6 q+ R
  25. - H: S2 ^. ~, d6 F0 v4 U* [$ U
  26.             if (extension == ".mp4" || extension == ".avi" || extension == ".mp3"). `% P% g, _6 _0 k6 f0 M6 F3 m/ X6 ^! j
  27.             {
    . Q2 l8 u( \) S% u
  28.                 string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);1 j! E7 I" |4 R$ \
  29.                 string destinationFilePath = Path.Combine(destinationFolder, relativePath);
    8 G  F  u1 q+ k/ s5 |. M0 R
  30.                 destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");2 ~3 Z/ H; a9 g$ f; @- N0 R. _# q

  31. 2 y' a; [- W' r
  32.                 Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
    ; ]3 B. l% O( C) B9 h

  33. 3 S! a; i' q/ ]+ n( C
  34.                 string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";  ~' i. B1 o# D! {9 E. S
  35.   X4 h( v, Z. G1 `) U. O
  36.                 Process ffmpegProcess = new Process! V2 {; L4 m0 n2 W4 i3 H4 y
  37.                 {
    * I$ G2 G9 t5 W) M
  38.                     StartInfo = new ProcessStartInfo# I1 D! p- ^7 x) U/ V
  39.                     {- M2 }* u2 u6 R4 G
  40.                         FileName = "ffmpeg64.exe",
    0 t+ X4 Y# |, Y5 y( c' ^- @' Y
  41.                         Arguments = ffmpegArgs,
    3 O+ T8 N. |  h3 N8 D! X' [
  42.                         UseShellExecute = false,
    & {7 \( n+ g2 J- t
  43.                         RedirectStandardError = true,
    % ?: }6 F/ C+ X  {& Y/ x; A: \
  44.                         CreateNoWindow = true9 _& t4 O1 L3 P0 r
  45.                     }/ E0 v+ X. X9 o3 {
  46.                 };
    : @% Z6 w: D2 _7 |( Y, I- A, e- S
  47. 4 O, Y" m7 f* }- P0 o' H3 u2 R- b% _
  48.                 ffmpegProcess.Start();
    / v; W1 s* m4 X. K
  49. $ B, X" T' T5 W
  50.                 while (!ffmpegProcess.StandardError.EndOfStream)
    ; p; F6 G( G% y- G1 w
  51.                 {
    2 z0 X7 q/ e) |; Z
  52.                     string line = ffmpegProcess.StandardError.ReadLine();
    # t/ {& _3 l) U; K
  53.                     Console.WriteLine(line);
    6 _2 G! p' B2 t! ]- U+ \' h
  54.                 }
    6 G0 P, y9 i  e

  55. 2 l. N7 |' Q$ e) b2 H6 K* f9 ]
  56.                 ffmpegProcess.WaitForExit();& R% X* c. n' T$ q2 O2 x( N: Z
  57.             }
    ) m' n4 ^( Z! ~! \! u/ p8 K# v, v7 S
  58.         }% R3 ?/ Q- k, I
  59.     }! w- s# |/ Z# y& v
  60. }
    9 B+ ~! _) q; p/ v1 B- d$ I5 p' @0 _4 p
复制代码
- p3 Q" a  i3 S8 L# B) T

$ G2 E6 i# }5 `
# p6 X8 l# v  Q( z3 C9 Z
9 w  J0 d+ r! g
作者: 星之韶华    时间: 2025-4-4 01:05
学习学习一下




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