upload_file.py
1.58 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
import os
import subprocess
import requests
def download_audio(url, output_path):
"""Download audios file from URL."""
response = requests.get(url)
with open(output_path, 'wb') as f:
f.write(response.content)
def cut_audio(input_path, output_base_path, start_times, durations):
"""Cut audios into multiple clips using FFmpeg."""
if not os.path.exists(os.path.dirname(output_base_path)):
os.makedirs(os.path.dirname(output_base_path))
for index, (start_time, duration) in enumerate(zip(start_times, durations), 1):
output_path = f"{output_base_path}{index}.mp3"
cmd = [
'ffmpeg',
'-ss', str(start_time),
'-i', input_path,
'-t', str(duration),
'-c', 'copy',
output_path
]
subprocess.run(cmd, check=True)
def main():
# Replace these with your URLs and desired output directory
audio_url = 'https://smartcard-1253080096.cos.ap-guangzhou.myqcloud.com/audio/806599C6309C_1710219356_806599C6309C-1710219356.MP3'
output_directory = 'D:/software/python/project/funasr_web/results/1best_recog'
# Time offsets and durations for each clip
start_times = [0, 10, 20]
durations = [10, 10, 10]
# Download the audios file
audio_file_name = os.path.basename(audio_url)
audio_file_path = os.path.join(output_directory, audio_file_name)
download_audio(audio_url, audio_file_path)
# Cut the audios file into clips
cut_audio(audio_file_path, output_directory + 'clip_', start_times, durations)
if __name__ == '__main__':
main()