-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.html
More file actions
180 lines (155 loc) · 9.37 KB
/
Copy path1.html
File metadata and controls
180 lines (155 loc) · 9.37 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VideoSRT Transcription1</title>
<script src="https://cdn.jsdelivr.net/npm/@ffmpeg/ffmpeg@0.10.1/dist/ffmpeg.min.js"></script>
<style>
/* 进度条样式 */
#progressContainer {
width: 100%;
background-color: #ddd;
}
#progressBar {
width: 1%;
height: 30px;
background-color: #4CAF50;
text-align: center;
color: white;
}
#progressBarContainer {
width: 100%;
background-color: #ddd;
}
#progressBar {
width: 0%;
height: 30px;
background-color: #4CAF50;
text-align: center;
line-height: 30px;
color: white;
}
</style>
</head>
<body>
<input type="file" id="fileUploader" accept="video/*" />
<button id="convertBtn">Convert to MP3</button>
<div id="progressContainer" style="display:none;">
<div id="progressBar">0%</div>
</div>
<button id="downloadBtn" style="display:none;">Download MP3</button>
<audio id="audioPlayer" controls style="display:none;"></audio> <!-- 添加音频播放器 -->
<br>
<h1>MP3 to SRT Transcription</h1>
<select id="mp3language" class=""><option value="">--</option><option value="af">af</option><option value="am">am</option><option value="ar">ar</option><option value="as">as</option><option value="az">az</option><option value="ba">ba</option><option value="be">be</option><option value="bg">bg</option><option value="bn">bn</option><option value="bo">bo</option><option value="br">br</option><option value="bs">bs</option><option value="ca">ca</option><option value="cs">cs</option><option value="cy">cy</option><option value="da">da</option><option value="de">de</option><option value="el">el</option><option value="en">en</option><option value="es">es</option><option value="et">et</option><option value="eu">eu</option><option value="fa">fa</option><option value="fi">fi</option><option value="fo">fo</option><option value="fr">fr</option><option value="gl">gl</option><option value="gu">gu</option><option value="ha">ha</option><option value="haw">haw</option><option value="he">he</option><option value="hi">hi</option><option value="hr">hr</option><option value="ht">ht</option><option value="hu">hu</option><option value="hy">hy</option><option value="id">id</option><option value="is">is</option><option value="it">it</option><option value="ja">ja</option><option value="jw">jw</option><option value="ka">ka</option><option value="kk">kk</option><option value="km">km</option><option value="kn">kn</option><option value="ko">ko</option><option value="la">la</option><option value="lb">lb</option><option value="ln">ln</option><option value="lo">lo</option><option value="lt">lt</option><option value="lv">lv</option><option value="mg">mg</option><option value="mi">mi</option><option value="mk">mk</option><option value="ml">ml</option><option value="mn">mn</option><option value="mr">mr</option><option value="ms">ms</option><option value="mt">mt</option><option value="my">my</option><option value="ne">ne</option><option value="nl">nl</option><option value="nn">nn</option><option value="no">no</option><option value="oc">oc</option><option value="pa">pa</option><option value="pl">pl</option><option value="ps">ps</option><option value="pt">pt</option><option value="ro">ro</option><option value="ru">ru</option><option value="sa">sa</option><option value="sd">sd</option><option value="si">si</option><option value="sk">sk</option><option value="sl">sl</option><option value="sn">sn</option><option value="so">so</option><option value="sq">sq</option><option value="sr">sr</option><option value="su">su</option><option value="sv">sv</option><option value="sw">sw</option><option value="ta">ta</option><option value="te">te</option><option value="tg">tg</option><option value="th">th</option><option value="tk">tk</option><option value="tl">tl</option><option value="tr">tr</option><option value="tt">tt</option><option value="uk">uk</option><option value="ur">ur</option><option value="uz">uz</option><option value="vi">vi</option><option value="yi">yi</option><option value="yo">yo</option><option value="yue">yue</option><option value="zh">zh</option></select>
<button id="transcribeButton">Transcribe to SRT</button>
<br>
<div id="mp3ProgressBarContainer" style="display: none;">
<div id="mp3ProgressBar">0%</div>
</div>
<br>
<a id="downloadSRTLink" style="display: none;">Download SRT</a>
<br>
<textarea id="transcriptionResult"></textarea>
<script>
const { createFFmpeg, fetchFile } = FFmpeg;
const ffmpeg = createFFmpeg({
log: true,
progress: ({ ratio }) => {
// 更新进度条
const progress = Math.floor(ratio * 100);
const progressBar = document.getElementById('progressBar');
progressBar.style.width = progress + '%';
progressBar.textContent = progress + '%';
}
});
const mp3Ffmpeg = createFFmpeg({
log: true,
progress: ({ ratio }) => {
const progressBar = document.getElementById('mp3ProgressBar');
progressBar.style.width = `${(ratio * 100).toFixed(2)}%`;
progressBar.innerText = `${(ratio * 100).toFixed(2)}%`;
}
});
document.getElementById('convertBtn').addEventListener('click', async () => {
const fileUploader = document.getElementById('fileUploader');
const files = fileUploader.files;
if (files.length === 0) {
alert("Please select a file.");
return;
}
const file = files[0];
// 显示进度条容器
document.getElementById('progressContainer').style.display = 'block';
const tempFileName = `temp_${Date.now()}`;
const outputFileName = `${tempFileName}.mp3`;
if (!ffmpeg.isLoaded()) await ffmpeg.load();
ffmpeg.FS('writeFile', tempFileName, await fetchFile(file));
await ffmpeg.run('-i', tempFileName, '-vn', '-ar', '44100', '-ac', '2', '-b:a', '192k', outputFileName);
const data = ffmpeg.FS('readFile', outputFileName);
const mp3Blob = new Blob([data.buffer], { type: 'audio/mpeg' });
const url = URL.createObjectURL(mp3Blob);
// 设置下载按钮
const downloadBtn = document.getElementById('downloadBtn');
downloadBtn.onclick = function() { // 使用onclick事件监听
const a = document.createElement('a');
a.href = url;
a.download = `${file.name.split('.')[0]}.mp3`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
downloadBtn.style.display = 'block'; // 显示下载按钮
// 设置音频播放器
const audioPlayer = document.getElementById('audioPlayer');
audioPlayer.src = url;
audioPlayer.style.display = 'block'; // 显示音频播放器
// Cleanup
document.getElementById('progressContainer').style.display = 'none'; // 转换完成后隐藏进度条
});
document.getElementById('transcribeButton').addEventListener('click', async () => {
const fileUploader = document.getElementById('fileUploader');
const files = fileUploader.files;
if (files.length === 0) {
alert("Please select a file.");
return;
}
const file = files[0];
transcribeButton.disabled = true;
transcriptionResult.textContent = 'Transcribing, please wait...';
if (!mp3Ffmpeg.isLoaded()) {
await mp3Ffmpeg.load();
}
mp3Ffmpeg.FS('writeFile', file.name, await fetchFile(file));
await mp3Ffmpeg.run('-i', file.name, '-vn', '-ar', '44100', '-ac', '2', '-b:a', '192k', 'output.mp3');
const data = mp3Ffmpeg.FS('readFile', 'output.mp3');
const mp3Blob = new Blob([data.buffer], { type: 'audio/mpeg' });
const formData = new FormData();
formData.append('audio_file', mp3Blob, 'output.mp3');
// 获取<select>元素
var selectElement = document.getElementById("mp3language");
// 获取选中的值
var selectedValue = selectElement.value;
// 打印选中的值
console.log(selectedValue);
try {
const response = await fetch('/asr?encode=true&task=transcribe&language='+selectedValue+'&initial_prompt=%E8%BF%99%E6%98%AF%E4%B8%80%E4%B8%AA%E4%BB%A5%E4%BC%9A%E8%AE%AE%E5%BD%95%E9%9F%B3%EF%BC%8C%E4%B8%BB%E8%A6%81%E8%AF%AD%E8%A8%80%E4%B8%BA%E6%99%AE%E9%80%9A%E8%AF%9D&word_timestamps=false&output=srt', {
method: 'POST',
body: formData
});
const result = await response.text();
transcriptionResult.textContent = result || 'Transcription failed, please try again.';
// 显示下载SRT按钮
const downloadSRTLink = document.getElementById('downloadSRTLink');
downloadSRTLink.style.display = 'block';
downloadSRTLink.href = `data:text/plain;charset=utf-8,${encodeURIComponent(result)}`;
downloadSRTLink.download = `${file.name.split('.')[0]}.srt`;
} catch (error) {
transcriptionResult.textContent = 'Transcription error, please try again.';
} finally {
transcribeButton.disabled = false;
}
});
</script>
</body>
</html>