8 ^6 U) `# i& h+ ?* k
为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的! m$ m% E3 J/ |- Y8 ? q3 m" o& J
sampleRate为16000的格式。
( ^ Z+ v8 h' H9 Q% f6 w5 F# t: V本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。1 p- Y2 j# P) s _5 H$ X
执行后,会在目标目录生成和源文件相同目录结构的wave文件,( H1 _- ]) s- o+ R( c/ r
供语音识别之用。9 h) z- g7 A; o+ G2 A
" i( O% n5 b. [' n
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,
# O( M! Q# h ~/ B5 Q可以从以下软件中获得。8 {( Z% X: ?# u( V
https://softaro.net/ffmpeggui/
( ~5 B) e w, j+ ~ c7 L8 W+ b |$ k4 h5 g
工具代码如下,vs2022编译即可4 f$ n2 @# G$ c/ O7 `
- using System;
- ?* E- r# K0 Z# N$ m3 v - using System.Diagnostics;7 |# l# i H3 `6 F: ?
- using System.IO;" w: ?2 u0 e9 C3 q) ~$ Q( E, u, M
% M; H$ Y' l# O. K# y- class Program6 {, N: y, ^ q5 c1 P
- {- N4 X( X: t8 B0 ]/ h6 v+ F
- static void Main(string[] args)
3 S; s. M2 U5 q - {6 c& f' A6 r/ p; |
- Console.WriteLine("cvtWave");
/ g+ A ?2 \) x" ?7 O - Console.WriteLine("请输入源文件夹路径 (folder1):");3 ^+ M) n- a/ W/ j
- string sourceFolder = Console.ReadLine();9 ^6 h4 J' \% T' I
- Console.WriteLine("请输入目标文件夹路径 (folder2):");
" I0 K) N" n" m3 [" R - string destinationFolder = Console.ReadLine();
; ?* Q! D* B, D3 a0 l0 m; B - * r! X$ s% a- v' W. N0 F) T
- ConvertFilesToWAV(sourceFolder, destinationFolder);& {. k+ C" Q5 q3 t2 z N
- 3 I# ~# b o# f7 A4 V2 W
- Console.WriteLine("转换完成.");/ ~8 q; P3 s# Y& ^' s' X
- }
4 Z2 X0 U2 D9 M9 K4 v% q9 y - * @9 l" m6 U5 S- g; L% O9 z4 g( U9 Y
- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
, r+ \( U8 m& o4 E5 f - {
0 j d; i! T# @! F7 P% _' Z3 J - foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories)), m1 c" c3 A; X }5 T3 n" y
- {5 H- d- [& l/ X
- string extension = Path.GetExtension(sourceFilePath).ToLower();
4 D e5 x; ?$ o
9 H6 Y; f1 q6 M# f+ V* o L- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")) I5 m" Y# m1 Q: K! J8 T
- {- z9 i- P* u7 F, [+ i3 _9 d% u: w
- string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);
8 ^' C4 U% k, Y& |0 ? - string destinationFilePath = Path.Combine(destinationFolder, relativePath);8 C# t5 X4 F; y, Z0 G9 m0 K# b) B0 H
- destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");5 a4 r1 w1 R t" |9 d7 ]
- 2 V7 Q5 H4 L* C3 f
- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
& P9 T# g+ v! t3 w - Y+ W, ~$ U3 O6 v5 o4 t H" u
- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";
8 z3 k- W- \4 v7 z3 D) O
& e: a3 X, z) l% S) u% t$ s- Process ffmpegProcess = new Process
+ N% O7 i J! m4 X7 m - {
$ X- J; X8 |4 R( J% X1 ? - StartInfo = new ProcessStartInfo
% M' B* H4 X3 l - {
/ v# v! i' E; ?) l3 ` - FileName = "ffmpeg64.exe",
6 ~5 L6 S$ a" {1 i - Arguments = ffmpegArgs,4 M/ G* [6 u- G5 d3 V
- UseShellExecute = false,
: S @9 R1 B4 O; b$ v# P, J - RedirectStandardError = true,
' v! J0 B( Y9 w" }$ P - CreateNoWindow = true* z: k7 x# p' H( H8 c. L* a8 H
- }
, r" Z: P4 `% W) e& v7 i - };7 V' _# T5 t8 Y- I$ l
- / Y8 ~3 F0 L! `4 ^* f
- ffmpegProcess.Start();8 ^, ~+ Y& m2 M3 r0 q
: J" v# ]; b3 P* l9 B5 L( c- while (!ffmpegProcess.StandardError.EndOfStream)
: i6 _1 ]) `4 t0 r S: o f- A ` - {# E( c8 O- n5 q5 c. U
- string line = ffmpegProcess.StandardError.ReadLine();4 {) I. w0 g2 ?& e+ ~
- Console.WriteLine(line);
# M+ I9 x# M- m: L - }
S- k) P: y# ^6 j. y
7 ~) b: E m# o+ q9 h" P* L- p- ffmpegProcess.WaitForExit();
& u% x' S; W! g - }$ E, C, b" ] P" c2 f* ^
- }
- @* v. `, |% R* V; u3 \ - }! l9 }' Z+ l! D# l& I1 u
- }
3 f1 M3 T- O, L) ?2 Q2 i
复制代码 5 Q3 S" K1 t5 K, u* W) C1 Q
) y& k7 J1 ?8 w5 y; @8 v8 k5 T9 H, j0 ?
! s' ?. h; x7 I: i
|