W" C3 D0 l/ S* D3 m, u
为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的1 M* c5 q+ p) U! h8 t. v \ Q9 z
sampleRate为16000的格式。3 ^: v) l( x/ B1 m+ g
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。
. o6 b8 O' j! d& |! b8 N执行后,会在目标目录生成和源文件相同目录结构的wave文件,: t9 |9 W& C1 U* x. I; [( ?( t% e
供语音识别之用。
1 |3 \- B" T# u% q8 ?5 }, R+ ~5 B+ P6 w3 K
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,. a/ u& m1 M3 r: _3 j% K7 E2 i
可以从以下软件中获得。
$ K* z! O' M( e3 Ihttps://softaro.net/ffmpeggui/
+ j: m3 h- O( N4 e; _
7 m. D- d9 [0 y* `5 W* h8 F6 a工具代码如下,vs2022编译即可
. j& ^) `$ d" v6 s- using System;
1 M: i" a9 R P8 s) m6 y' A - using System.Diagnostics;0 x8 u0 ~' x- b
- using System.IO;
9 N3 [0 Y; |# K, G
7 M) P, }# h! z3 o- class Program
% y1 b5 P* Z4 A$ ?% u: Z$ m - {3 D. F l' b, C* V! F- N
- static void Main(string[] args)4 Z2 J: v4 _0 N# E+ \ w1 o
- {
$ ]$ A0 Z T7 |7 {6 X6 m - Console.WriteLine("cvtWave");/ N( d1 s8 P/ ]# X' l! G8 l4 s
- Console.WriteLine("请输入源文件夹路径 (folder1):");. n$ d) o4 w% W6 q( F2 S& ~
- string sourceFolder = Console.ReadLine();6 R- {+ B# f* D l
- Console.WriteLine("请输入目标文件夹路径 (folder2):");
' v T4 C) i$ `& w# T! ]( I2 A - string destinationFolder = Console.ReadLine();1 e2 x' W6 n% k: B: n/ I
- , e- s; Z' }6 }
- ConvertFilesToWAV(sourceFolder, destinationFolder);
+ s9 }- p$ l, {1 ?; F
- U: B b4 D1 z6 d% C }6 U- Console.WriteLine("转换完成.");
- E1 F& _' M- d0 i - }, H/ Z( D9 W, [3 n6 v' D
- 0 K% ?: n% }& s+ Q }! `; F3 L
- static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
; A- l D4 k! D2 H9 a - {+ N/ r8 I, U% l7 h6 t$ p) H
- foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
& l" G& ~$ j* M7 K7 e& l+ Y - {
, B; T$ J, O: s8 o" C* v- x" e - string extension = Path.GetExtension(sourceFilePath).ToLower();& B' [, t# s# D P
- ) {# r a/ v2 ]3 R2 ?7 q5 B$ c8 q
- if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")
: i8 ? K' f& X - {
/ t4 O+ ]+ @3 `5 D- C - string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);3 z4 M9 w* S# g3 S3 n
- string destinationFilePath = Path.Combine(destinationFolder, relativePath);
/ l5 L) Q( |7 U0 y' k( R - destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");. M7 p( u9 d6 q3 L
- 0 b" a9 j1 z0 ~9 g2 H9 j' y
- Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));% k8 s! N. [; o, ^$ g* m
- 3 h5 o$ b) f ?4 k6 ?/ E
- string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";
% W8 N% U# J' ]! [. ]/ y `. H* ?! r
. r, q0 a: y6 j( ~- Process ffmpegProcess = new Process) M0 s$ e; X. z9 d% p
- {; ]' S+ w- i2 e+ [ ]7 @
- StartInfo = new ProcessStartInfo
" K; {) A, o& Z - {
* v' D4 |7 R& W* B# z& Y g - FileName = "ffmpeg64.exe",7 K5 r8 U6 B4 d# I x9 L
- Arguments = ffmpegArgs,! N) G s0 E5 e
- UseShellExecute = false,
8 A3 W( U3 e2 | - RedirectStandardError = true,
* v+ _2 C8 A0 f1 ?9 ]4 V% G' Z/ ?, F - CreateNoWindow = true, g0 l* L' S3 {- w
- }
, p ~2 _4 ?% D5 Z/ ? - };0 F' W7 N6 {; t, \* {- ^
- 3 X R6 a3 S# u
- ffmpegProcess.Start();5 ~4 Z" Q" J$ F4 F$ V
* l# a6 U9 R) Q$ }. V' e' A: p- while (!ffmpegProcess.StandardError.EndOfStream)2 b5 B5 d7 }+ ^, H
- {# ^$ z: E5 ?( N3 G! y L* c% Q
- string line = ffmpegProcess.StandardError.ReadLine();
, t! [$ n4 c7 W" u0 l; T - Console.WriteLine(line);* ^7 q& s: M- A3 z- J! p% z5 B; l5 U
- }
x% ^5 l! ~2 a3 o J3 Y - ! `5 P* U& H/ h% l9 f: J
- ffmpegProcess.WaitForExit(); K# j& H' w1 h' \7 k. Z! o' I" r1 |
- }
" r W& j" n6 g - }- w$ s# E1 |3 }. X% b
- }
& C4 }9 u1 e' @/ j! J9 e' _- z4 I - }/ `1 y- w0 d7 _
复制代码
, c) H/ _" c! g9 J( x
8 e* g4 E, ^/ s7 M7 _4 L2 n: V: T2 n6 X& S# m
' M; B' c' K( Y |