冒险解谜游戏中文网 ChinaAVG
标题:
【汉化工具系列 #1】指定wave格式转换工具(cvtWave)
[打印本页]
作者:
shane007
时间:
2023-9-4 08:56
标题:
【汉化工具系列 #1】指定wave格式转换工具(cvtWave)
- o, [! C5 _; Z9 I* M& y
为了配合whisper语音识别,需要将mp3,wav,mp4等格式转换为其所需要的
" o5 P: C2 S6 E$ L" ~
sampleRate为16000的格式。
3 v9 \0 H0 w* N7 W* b( w
本工具为命令行工具,根据命令提示輸入源文件和目标文件的目录。
+ B0 F3 b" B/ k' \! @
执行后,会在目标目录生成和源文件相同目录结构的wave文件,
, g' m l2 X+ ~4 c
供语音识别之用。
& y, l# b; S! S9 s0 x- a/ h5 @
6 O2 L8 g$ i7 y9 j3 f/ {
本工具需要配合ffmpeg64.exe,Processing.NDI.Lib.x64.dll,
9 e2 w& F% |7 y
可以从以下软件中获得。
9 Q4 r0 x* ^2 I+ w2 U- ]
https://softaro.net/ffmpeggui/
4 U4 Y }6 o$ w# F
& m% @1 A& B9 q1 u+ u7 o
工具代码如下,vs2022编译即可
' {3 q+ H. e4 _
using System;
/ q3 r |, _' t! ]; b4 |5 Z1 u# x
using System.Diagnostics;
* K+ U4 a }5 D9 E% M/ ~, e2 x
using System.IO;
: s' `" f' i, m, j; o! Q
6 \0 o1 k- `) {; g$ Z
class Program
8 J* S7 q2 p$ {2 [) K
{
- u- N. |- O% q* c
static void Main(string[] args)
8 H r. Y( a8 G! ]; C+ n) |
{
' V/ O" `* R/ J8 w- L
Console.WriteLine("cvtWave");
' g4 @. ^; X) ?3 b
Console.WriteLine("请输入源文件夹路径 (folder1):");
! C0 a4 B& \4 F! N
string sourceFolder = Console.ReadLine();
3 u i7 q" n: V( O
Console.WriteLine("请输入目标文件夹路径 (folder2):");
. i& T3 Q" `' Z8 r: J+ w4 @% J! l
string destinationFolder = Console.ReadLine();
! P6 g. h7 |# z' _
5 f! H: R7 f/ _: y
ConvertFilesToWAV(sourceFolder, destinationFolder);
4 @7 B3 w5 q# V, p' g u+ [
0 j* f/ y% o9 T) A( u3 C" M( R- _ h
Console.WriteLine("转换完成.");
' G( K+ j9 S5 N% q. m
}
2 v. `5 R) m) W
, C6 T5 l# p! Z1 d
static void ConvertFilesToWAV(string sourceFolder, string destinationFolder)
1 p, F, Y4 j3 \. W# Y) h# a
{
+ ]' l% z3 p2 D i1 Y
foreach (var sourceFilePath in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
; O" m% S% L' _& D) H; f2 U+ g
{
* j; V6 a8 n7 Y: C! c
string extension = Path.GetExtension(sourceFilePath).ToLower();
9 Q" i f* s% P% w; r: g
# y$ k0 ?7 l: p: |
if (extension == ".mp4" || extension == ".avi" || extension == ".mp3")
" Q3 N1 F! K* m5 {; j h% i
{
9 b% k+ n! {. l; @3 v. d; }
string relativePath = Path.GetRelativePath(sourceFolder, sourceFilePath);
, y; f* r- p; _- ^3 I+ m
string destinationFilePath = Path.Combine(destinationFolder, relativePath);
$ a; C$ T- h3 }5 q; R2 L/ I
destinationFilePath = Path.ChangeExtension(destinationFilePath, ".wav");
$ |9 H8 J3 K# t. {9 D% ]
8 ]( @8 j0 Y2 _0 {2 {( R
Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));
5 b: z( F6 Y5 L2 f0 Q0 I& @, ^
, @' v: D2 s" @( O0 P
string ffmpegArgs = $"-i \"{sourceFilePath}\" -f wav -vn -b:a 256K -ar 16000 -y \"{destinationFilePath}\"";
2 ^9 u* ^, g. N4 `# r
" Z- A7 { `1 Z9 o
Process ffmpegProcess = new Process
. `; k ?8 _! U6 e
{
5 w! D, `( W) \2 o
StartInfo = new ProcessStartInfo
. t+ d7 P" ^! g' Z3 k$ q
{
4 n, z* M8 x1 B2 c' ^, h
FileName = "ffmpeg64.exe",
: g+ A1 H+ v: t n
Arguments = ffmpegArgs,
% g. X( n0 Q9 d9 T; m9 P
UseShellExecute = false,
- g8 S5 r0 W& ^% p9 w6 ~" t4 v
RedirectStandardError = true,
9 Y$ {: e6 I0 |2 Q& v
CreateNoWindow = true
( x& V, J6 M! M/ g7 z6 p4 ]' q
}
$ K7 e" [' Z. l# |5 L
};
6 X& h% _% C* c" ?
+ G0 @" U* v: N9 N' R0 p: l
ffmpegProcess.Start();
: g0 W6 H1 h; s; a7 ?) W" x
% M. P, e" M9 {, g4 W/ Q, J* S- Y
while (!ffmpegProcess.StandardError.EndOfStream)
* l& L+ f5 `; S h3 u* T4 V
{
+ q7 Y: \ E# u& O0 S {
string line = ffmpegProcess.StandardError.ReadLine();
0 d, N3 C# u, d0 |# o8 p$ s: L" e3 n
Console.WriteLine(line);
% n* H9 y1 H3 ?( M3 a
}
8 s3 ], A: _7 i5 W0 c% I# [( x0 K
( c6 A4 }) {0 L1 c4 B7 e
ffmpegProcess.WaitForExit();
* v; i: F! F! `- \( ~+ D3 T
}
: j8 V5 B: s" v4 ?4 r' g/ S
}
0 @6 h( J! r; U( s
}
$ o! ]$ l7 O2 ^1 d( z4 ^6 r" K( ?
}
: [7 u# |1 Q1 S, Z1 e* L
复制代码
7 {+ l: H' \. A5 o: ?5 d, C2 @
2 w }$ U, Z0 n2 `7 c5 d5 u
4 `0 ?4 c5 s: D1 p2 a# p' [
+ J( j8 U, c! [7 ~. e& ?
欢迎光临 冒险解谜游戏中文网 ChinaAVG (https://chinaavg.com/)
Powered by Discuz! X3.2