-
Notifications
You must be signed in to change notification settings - Fork 1
/
大量短音频免费快速转文字.html
537 lines (375 loc) · 40.7 KB
/
大量短音频免费快速转文字.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"leeyuxun.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":true,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"./public/search.xml"};
</script>
<meta name="description" content="前言最近做起工具人,对大量音频进行文字标注。工作太枯燥,费时,就写了一个脚本,借助迅捷音频转文字在线工具,个人感觉在一定程度上提高了效率">
<meta property="og:type" content="article">
<meta property="og:title" content="大量短音频免费快速转文字">
<meta property="og:url" content="https://leeyuxun.github.io/%E5%A4%A7%E9%87%8F%E7%9F%AD%E9%9F%B3%E9%A2%91%E5%85%8D%E8%B4%B9%E5%BF%AB%E9%80%9F%E8%BD%AC%E6%96%87%E5%AD%97.html">
<meta property="og:site_name" content="Leeyuxun の note">
<meta property="og:description" content="前言最近做起工具人,对大量音频进行文字标注。工作太枯燥,费时,就写了一个脚本,借助迅捷音频转文字在线工具,个人感觉在一定程度上提高了效率">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-15T04:44:22.000Z">
<meta property="article:modified_time" content="2023-05-07T07:37:53.528Z">
<meta property="article:author" content="李钰璕">
<meta property="article:tag" content="python">
<meta property="article:tag" content="音频转文字">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="https://leeyuxun.github.io/%E5%A4%A7%E9%87%8F%E7%9F%AD%E9%9F%B3%E9%A2%91%E5%85%8D%E8%B4%B9%E5%BF%AB%E9%80%9F%E8%BD%AC%E6%96%87%E5%AD%97.html">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : false,
isPost : true,
lang : 'zh-CN'
};
</script>
<title>大量短音频免费快速转文字 | Leeyuxun の note</title>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-V3499K2XZY"></script>
<script>
if (CONFIG.hostname === location.hostname) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-V3499K2XZY');
}
</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?4d72a66931dff6410b32974da2e3df61";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<h1 class="site-title">Leeyuxun の note</h1>
<span class="logo-line-after"><i></i></span>
</a>
<p class="site-subtitle" itemprop="description">BUPT | SCSS</p>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
<i class="fa fa-search fa-fw fa-lg"></i>
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="main-menu menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>
</li>
<li class="menu-item menu-item-links">
<a href="/links/" rel="section"><i class="fa fa-link fa-fw"></i>友链</a>
</li>
<li class="menu-item menu-item-search">
<a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
</a>
</li>
</ul>
</nav>
<div class="search-pop-overlay">
<div class="popup search-popup">
<div class="search-header">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<div class="search-input-container">
<input autocomplete="off" autocapitalize="off"
placeholder="搜索..." spellcheck="false"
type="search" class="search-input">
</div>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
</div>
<div id="search-result">
<div id="no-result">
<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
</div>
</div>
</div>
</div>
</div>
</header>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content post posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://leeyuxun.github.io/%E5%A4%A7%E9%87%8F%E7%9F%AD%E9%9F%B3%E9%A2%91%E5%85%8D%E8%B4%B9%E5%BF%AB%E9%80%9F%E8%BD%AC%E6%96%87%E5%AD%97.html">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.png">
<meta itemprop="name" content="李钰璕">
<meta itemprop="description" content="安全学习笔记">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Leeyuxun の note">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
大量短音频免费快速转文字
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-03-15 12:44:22" itemprop="dateCreated datePublished" datetime="2020-03-15T12:44:22+08:00">2020-03-15</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2023-05-07 15:37:53" itemprop="dateModified" datetime="2023-05-07T15:37:53+08:00">2023-05-07</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/personal-tools/" itemprop="url" rel="index"><span itemprop="name">personal tools</span></a>
</span>
</span>
<span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span id="busuanzi_value_page_pv"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>最近做起工具人,对大量音频进行文字标注。工作太枯燥,费时,就写了一个脚本,借助迅捷音频转文字在线工具,个人感觉在一定程度上提高了效率<span id="more"></span></p>
<h1 id="过程"><a href="#过程" class="headerlink" title="过程"></a>过程</h1><p>很简单</p>
<ol>
<li>将大量短音频合并,中间用特殊的音频分隔</li>
<li>将大的音频分隔成固定大小的音频(迅捷在线语音转换一次最多只能免费转换20M的音频)</li>
<li>上传到迅捷音频转换页面,进行转换,并把txt文件下载</li>
<li>整理txt文件,删除标点和间隔语音转换的文字,并把所有文字合并到同一个txt文件里面</li>
</ol>
<h1 id="语言"><a href="#语言" class="headerlink" title="语言"></a>语言</h1><p>python 3.7</p>
<h1 id="引用库"><a href="#引用库" class="headerlink" title="引用库"></a>引用库</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> shutil</span><br><span class="line"><span class="keyword">from</span> pydub <span class="keyword">import</span> AudioSegment</span><br><span class="line"><span class="keyword">from</span> pydub.utils <span class="keyword">import</span> make_chunks</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> re</span><br></pre></td></tr></table></figure>
<p>具体原理相关库已经说明,可以使用pip直接下载,也可以手动下载安装,遇到问题直接Google求助,这里不再说明</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><p>脚本1,将音频合并切割并转换成txt</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> shutil</span><br><span class="line"><span class="keyword">from</span> pydub <span class="keyword">import</span> AudioSegment</span><br><span class="line"><span class="keyword">from</span> pydub.utils <span class="keyword">import</span> make_chunks</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"></span><br><span class="line">dir_parent = <span class="string">'D:\\测试音频'</span> <span class="comment">#音频存放位置</span></span><br><span class="line">dir_letter = dir_parent + <span class="string">'\\合并后的音频'</span> <span class="comment">#合并后的音频存放的位置</span></span><br><span class="line">dir_letter_letter = dir_parent + <span class="string">'\\切割后的音频'</span> <span class="comment">#切割后的音频存放的位置</span></span><br><span class="line">dir_letter_txt = dir_parent + <span class="string">'\\转换后的txt文件'</span> <span class="comment">#下载转换后的txt文件存放的位置</span></span><br><span class="line">add_sound = AudioSegment.from_wav(<span class="string">'sample.wav'</span>) <span class="comment">#sample.wav是间隔音频,和脚本放在同一目录</span></span><br><span class="line">sum_sound = add_sound</span><br><span class="line"></span><br><span class="line"><span class="comment"># 分别和并同一文件夹下的所有音频</span></span><br><span class="line"><span class="keyword">for</span> path,dir_list,file_list <span class="keyword">in</span> os.walk(dir_parent):</span><br><span class="line"> <span class="keyword">for</span> dir_name <span class="keyword">in</span> dir_list:</span><br><span class="line"> dir_child = dir_parent + <span class="string">'\\'</span> + dir_name</span><br><span class="line"> <span class="keyword">for</span> path,dir_list,file_list <span class="keyword">in</span> os.walk(dir_child):</span><br><span class="line"> <span class="keyword">for</span> file_name <span class="keyword">in</span> file_list:</span><br><span class="line"> <span class="comment">#print(file_name)</span></span><br><span class="line"> front_path = os.path.join(path, file_name)</span><br><span class="line"> <span class="comment">#print(front_path)</span></span><br><span class="line"> front_sound = AudioSegment.from_wav(front_path)</span><br><span class="line"> sum_sound = sum_sound + front_sound + add_sound</span><br><span class="line"> sum_sound.export(dir_child+<span class="string">'已合并.wav'</span>,<span class="built_in">format</span>=<span class="string">"wav"</span>)</span><br><span class="line"> sum_sound = add_sound</span><br><span class="line"> <span class="built_in">print</span>(dir_child+<span class="string">'文件下的音频合并完成'</span>)</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 将处理过的音频放入同一文件夹内</span></span><br><span class="line">os.makedirs(dir_letter)</span><br><span class="line"><span class="keyword">for</span> file <span class="keyword">in</span> os.listdir(dir_parent):</span><br><span class="line"> <span class="keyword">if</span> os.path.isfile(dir_parent + <span class="string">'\\'</span> + file):</span><br><span class="line"> <span class="keyword">if</span> <span class="string">'已合并'</span> <span class="keyword">in</span> file:</span><br><span class="line"> shutil.move(dir_parent + <span class="string">'\\'</span> + file,dir_letter) </span><br><span class="line"><span class="built_in">print</span>(<span class="string">"所有音频合并完成"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 切割合并后的音频</span></span><br><span class="line">chunk_length_ms = <span class="number">325000</span> <span class="comment"># 分块的毫秒数(测试wav文件每个音频如果在325s内刚好不多于20M,当然也可以改成300s)</span></span><br><span class="line">os.makedirs(dir_letter_letter) <span class="comment"># 创建切割音频的文件夹</span></span><br><span class="line"><span class="keyword">for</span> path,dir_list,file_list <span class="keyword">in</span> os.walk(dir_letter):</span><br><span class="line"> <span class="keyword">for</span> file_name <span class="keyword">in</span> file_list:</span><br><span class="line"> audio = AudioSegment.from_file(dir_letter+<span class="string">'\\'</span>+file_name , <span class="string">"wav"</span>) </span><br><span class="line"> chunks = make_chunks(audio, chunk_length_ms) <span class="comment">#将音频切割</span></span><br><span class="line"> <span class="comment">#保存切割的音频到文件</span></span><br><span class="line"> <span class="comment"># 下面两行目的是对文件名进行简单处理,保证文件名都是数字,排序时方便整理,这个因文件名而异</span></span><br><span class="line"> file_name_change1 = file_name.strip(<span class="string">'已合并.wav'</span>)</span><br><span class="line"> file_name_change2 = file_name_change1.strip(<span class="string">'AAAAA'</span>) <span class="comment">#AAAAA是file_name的前缀</span></span><br><span class="line"> <span class="keyword">for</span> i, chunk <span class="keyword">in</span> <span class="built_in">enumerate</span>(chunks):</span><br><span class="line"> chunk_name = dir_letter_letter+<span class="string">"\\"</span>+file_name_change2+<span class="string">"{0}.wav"</span>.<span class="built_in">format</span>(i)</span><br><span class="line"> chunk.export(chunk_name, <span class="built_in">format</span>=<span class="string">"wav"</span>)</span><br><span class="line"> <span class="built_in">print</span> (<span class="string">"音频"</span>+file_name_change2+<span class="string">"切割完成"</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"所有音频切割完成"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 转换音频文件</span></span><br><span class="line"><span class="comment"># 更改firefox默认下载设置,不懂可自行Google</span></span><br><span class="line">profile = webdriver.FirefoxProfile()</span><br><span class="line">profile.set_preference(<span class="string">'browser.download.dir'</span>,dir_letter_letter)</span><br><span class="line">profile.set_preference(<span class="string">'browser.download.folderList'</span>,<span class="number">2</span>)</span><br><span class="line">profile.set_preference(<span class="string">'browser.download.manager.showWhenStarting'</span>,<span class="literal">False</span>)</span><br><span class="line">profile.set_preference(<span class="string">'browser.helperApps.neverAsk.saveToDisk'</span>,<span class="string">'text/plain'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 历遍文件夹内的文件并返回一个列表</span></span><br><span class="line">path_list = os.listdir(dir_letter_letter)</span><br><span class="line"><span class="comment"># 利用循环历遍path_list列表</span></span><br><span class="line"><span class="keyword">for</span> file_name <span class="keyword">in</span> path_list:</span><br><span class="line"> <span class="comment">#print(filename)</span></span><br><span class="line"> <span class="comment">#启动浏览器</span></span><br><span class="line"> driver1 = webdriver.Firefox(firefox_profile=profile)</span><br><span class="line"> driver1.get(<span class="string">"https://app.xunjiepdf.com/voice2text/"</span>) <span class="comment">#打开迅捷转换网页</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'开始上传'</span>+file_name+<span class="string">'音频文件'</span>)</span><br><span class="line"> upload = driver1.find_element_by_name(<span class="string">'file'</span>) <span class="comment">#寻找上传按钮</span></span><br><span class="line"> upload.send_keys(dir_letter_letter+<span class="string">'\\'</span>+file_name) <span class="comment">#上传文件</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'音频文件'</span>+file_name+<span class="string">'上传成功'</span>)</span><br><span class="line"> time.sleep(<span class="number">50</span>) <span class="comment">#睡眠50S等待音频转换完成</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'开始下载文件'</span>)</span><br><span class="line"> driver1.execute_script(<span class="string">"arguments[0].click();"</span>, driver1.find_element_by_xpath(<span class="string">"//a[contains(text(),'立即下载')]"</span>)) <span class="comment">#定位txt下载按钮并下载</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'文件下载成功'</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'所有文件下载完成'</span>)</span><br></pre></td></tr></table></figure>
<p>脚本2,整理txt</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> shutil</span><br><span class="line"><span class="keyword">from</span> pydub <span class="keyword">import</span> AudioSegment</span><br><span class="line"><span class="keyword">from</span> pydub.utils <span class="keyword">import</span> make_chunks</span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"></span><br><span class="line">dir_parent = <span class="string">'D:\\测试音频'</span> <span class="comment">#音频存放位置</span></span><br><span class="line">dir_letter = dir_parent + <span class="string">'\\合并后的音频'</span> <span class="comment">#合并后的音频存放的位置</span></span><br><span class="line">dir_letter_letter = dir_parent + <span class="string">'\\切割后的音频'</span> <span class="comment">#切割后的音频存放的位置</span></span><br><span class="line">dir_letter_txt = dir_parent + <span class="string">'\\转换后的txt文件'</span> <span class="comment">#下载转换后的txt文件存放的位置</span></span><br><span class="line">add_sound = AudioSegment.from_wav(<span class="string">'sample.wav'</span>) <span class="comment">#sample.wav是间隔音频,和脚本放在同一目录</span></span><br><span class="line">sum_sound = add_sound</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将所有txt文件合并</span></span><br><span class="line"><span class="keyword">for</span> file_name <span class="keyword">in</span> os.listdir(dir_letter_txt):</span><br><span class="line"> <span class="built_in">print</span>(file_name)</span><br><span class="line"> <span class="keyword">for</span> line <span class="keyword">in</span> <span class="built_in">open</span>(dir_letter_txt+<span class="string">'\\'</span>+file_name,encoding=<span class="string">'UTF-8'</span>):</span><br><span class="line"> f = <span class="built_in">open</span>(<span class="string">"result.txt"</span>,<span class="string">'a'</span>) <span class="comment"># result.txt是创建的和脚本位于同一目录下的存放所有转换文字的文件</span></span><br><span class="line"> f.write(line)</span><br><span class="line"> f.close()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 整理了txt文件内容</span></span><br><span class="line"><span class="comment"># 将文件中的句号转换成空格</span></span><br><span class="line">f=<span class="built_in">open</span>(<span class="string">'result.txt'</span>,<span class="string">'r'</span>)</span><br><span class="line">alllines=f.readlines()</span><br><span class="line">f.close()</span><br><span class="line">f=<span class="built_in">open</span>(<span class="string">'result.txt'</span>,<span class="string">'w+'</span>)</span><br><span class="line"><span class="keyword">for</span> eachline <span class="keyword">in</span> alllines:</span><br><span class="line"> a=re.sub(<span class="string">'。'</span>,<span class="string">' '</span>,eachline)</span><br><span class="line"> f.writelines(a)</span><br><span class="line">f.close()</span><br><span class="line"><span class="comment"># 将文件中的逗号转换成空格</span></span><br><span class="line">f=<span class="built_in">open</span>(<span class="string">'result.txt'</span>,<span class="string">'r'</span>)</span><br><span class="line">alllines=f.readlines()</span><br><span class="line">f.close()</span><br><span class="line">f=<span class="built_in">open</span>(<span class="string">'result.txt'</span>,<span class="string">'w+'</span>)</span><br><span class="line"><span class="keyword">for</span> eachline <span class="keyword">in</span> alllines:</span><br><span class="line"> a=re.sub(<span class="string">','</span>,<span class="string">' '</span>,eachline)</span><br><span class="line"> f.writelines(a)</span><br><span class="line">f.close()</span><br><span class="line"><span class="comment"># 将文件中的顿号转换成空格</span></span><br><span class="line">f=<span class="built_in">open</span>(<span class="string">'result.txt'</span>,<span class="string">'r'</span>)</span><br><span class="line">alllines=f.readlines()</span><br><span class="line">f.close()</span><br><span class="line">f=<span class="built_in">open</span>(<span class="string">'result.txt'</span>,<span class="string">'w+'</span>)</span><br><span class="line"><span class="keyword">for</span> eachline <span class="keyword">in</span> alllines:</span><br><span class="line"> a=re.sub(<span class="string">'、'</span>,<span class="string">''</span>,eachline)</span><br><span class="line"> f.writelines(a)</span><br><span class="line">f.close()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将间隔音频转换的文字转换成换行符,方便查看</span></span><br><span class="line">f=<span class="built_in">open</span>(<span class="string">'result.txt'</span>,<span class="string">'r'</span>) </span><br><span class="line">alllines=f.readlines()</span><br><span class="line">f.close()</span><br><span class="line">f=<span class="built_in">open</span>(<span class="string">'result.txt'</span>,<span class="string">'w+'</span>)</span><br><span class="line"><span class="keyword">for</span> eachline <span class="keyword">in</span> alllines:</span><br><span class="line"> a=re.sub(<span class="string">'AAAA'</span>,<span class="string">'\n'</span>,eachline) <span class="comment">#AAAA是间隔音频转换的文字内容</span></span><br><span class="line"> f.writelines(a)</span><br><span class="line">f.close()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">"txt处理完成,请查看result.txt文件"</span>)</span><br></pre></td></tr></table></figure>
<p>脚本写的比较匆忙,自己对python也不是很熟悉,没有使用函数,凑活用,直接是个单线程的程序,尝试处理八百多条音频时间二十分钟左右,识别率挺高,没有用多线程是因为脚本运行过程中多次调用firefox,担心程序崩溃。</p>
<h1 id="附"><a href="#附" class="headerlink" title="附"></a>附</h1><p>脚本文件已上传到<a target="_blank" rel="noopener" href="https://github.com/Leeyuxun/Audio-to-text">github</a>,可自行下载;</p>
<p>脚本成功运行的条件是音频放在多个文件夹里面,多个文件夹同时有放在同一个名为“测试音频”的文件夹里面,相当于是三级目录。</p>
</div>
<div>
<ul class="post-copyright">
<li class="post-copyright-author">
<strong>本文作者: </strong>李钰璕
</li>
<li class="post-copyright-link">
<strong>本文链接:</strong>
<a href="https://leeyuxun.github.io/%E5%A4%A7%E9%87%8F%E7%9F%AD%E9%9F%B3%E9%A2%91%E5%85%8D%E8%B4%B9%E5%BF%AB%E9%80%9F%E8%BD%AC%E6%96%87%E5%AD%97.html" title="大量短音频免费快速转文字">https://leeyuxun.github.io/大量短音频免费快速转文字.html</a>
</li>
<li class="post-copyright-license">
<strong>版权声明: </strong>本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处!
</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-tags">
<a href="/tags/python/" rel="tag"><i class="fa fa-tag"></i> python</a>
<a href="/tags/%E9%9F%B3%E9%A2%91%E8%BD%AC%E6%96%87%E5%AD%97/" rel="tag"><i class="fa fa-tag"></i> 音频转文字</a>
</div>
<div class="post-nav">
<div class="post-nav-item">
<a href="/dig%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8.html" rel="prev" title="dig简单使用">
<i class="fa fa-chevron-left"></i> dig简单使用
</a></div>
<div class="post-nav-item">
<a href="/simple-webmail.html" rel="next" title="simple webmail">
simple webmail <i class="fa fa-chevron-right"></i>
</a></div>
</div>
</footer>
</article>
</div>
<script>
window.addEventListener('tabs:register', () => {
let { activeClass } = CONFIG.comments;
if (CONFIG.comments.storage) {
activeClass = localStorage.getItem('comments_active') || activeClass;
}
if (activeClass) {
let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
if (activeTab) {
activeTab.click();
}
}
});
if (CONFIG.comments.storage) {
window.addEventListener('tabs:click', event => {
if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
let commentClass = event.target.classList[1];
localStorage.setItem('comments_active', commentClass);
});
}
</script>
</div>
<div class="toggle sidebar-toggle">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner">
<ul class="sidebar-nav motion-element">
<li class="sidebar-nav-toc">
文章目录
</li>
<li class="sidebar-nav-overview">
站点概览
</li>
</ul>
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
<div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%BF%87%E7%A8%8B"><span class="nav-number">2.</span> <span class="nav-text">过程</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%AF%AD%E8%A8%80"><span class="nav-number">3.</span> <span class="nav-text">语言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BC%95%E7%94%A8%E5%BA%93"><span class="nav-number">4.</span> <span class="nav-text">引用库</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81"><span class="nav-number">5.</span> <span class="nav-text">代码</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%99%84"><span class="nav-number">6.</span> <span class="nav-text">附</span></a></li></ol></div>
</div>
<!--/noindex-->
<div class="site-overview-wrap sidebar-panel">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="李钰璕"
src="/images/avatar.png">
<p class="site-author-name" itemprop="name">李钰璕</p>
<div class="site-description" itemprop="description">安全学习笔记</div>
</div>
<div class="site-state-wrap motion-element">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">89</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories/">
<span class="site-state-item-count">17</span>
<span class="site-state-item-name">分类</span></a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags/">
<span class="site-state-item-count">115</span>
<span class="site-state-item-name">标签</span></a>
</div>
</nav>
</div>
<div class="links-of-author motion-element">
<span class="links-of-author-item">
<a href="https://github.com/Leeyuxun" title="GitHub → https://github.com/Leeyuxun" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
</span>
<span class="links-of-author-item">
<a href="mailto:leeyuxun@163.com" title="E-Mail → mailto:leeyuxun@163.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
</span>
</div>
</div>
<div class="back-to-top motion-element">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
</div>
</aside>
<div id="sidebar-dimmer"></div>
</div>
</main>
<footer class="footer">
<div class="footer-inner">
<!--
<div class="copyright">
©
<span itemprop="copyrightYear">2023</span>
<span class="with-love">
<i class="fa fa-heart"></i>
</span>
<span class="author" itemprop="copyrightHolder">李钰璕</span>
</div>
-->
<div class="busuanzi-count">
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</div>
</div>
</footer>
</div>
<script src="/lib/anime.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js"></script>
<script src="/lib/velocity/velocity.min.js"></script>
<script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script>
<script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>
<script src="/js/local-search.js"></script>
</body>
</html>