Spaces:
Sleeping
Sleeping
add lib seaborn
Browse files- python/util/audio_to_mp3.py +0 -42
- python/util/to_mp3.py +76 -0
python/util/audio_to_mp3.py
DELETED
@@ -1,42 +0,0 @@
|
|
1 |
-
from moviepy.editor import *
|
2 |
-
import sys
|
3 |
-
# print('argv[1]:' + str(sys.argv[1]))
|
4 |
-
import os
|
5 |
-
from pydub import AudioSegment
|
6 |
-
|
7 |
-
|
8 |
-
def mp4_to_mp3(mp4_uri):
|
9 |
-
# Load the mp4 file
|
10 |
-
video = VideoFileClip(mp4_uri)
|
11 |
-
|
12 |
-
# Extract audio from video
|
13 |
-
video.audio.write_audiofile(mp4_uri + ".mp3")
|
14 |
-
|
15 |
-
|
16 |
-
def audio_to_wav(wav_uri, out_sample_rate=16000):
|
17 |
-
src = wav_uri
|
18 |
-
dst = wav_uri + ".wav"
|
19 |
-
if os.path.exists(dst): os.remove(dst)
|
20 |
-
|
21 |
-
# convert wav to mp3
|
22 |
-
sound = AudioSegment.from_file(src)
|
23 |
-
sound = sound.set_frame_rate(out_sample_rate)
|
24 |
-
sound.export(dst, format="wav")
|
25 |
-
|
26 |
-
print(' audio_to_wav: ', str(dst))
|
27 |
-
return dst
|
28 |
-
|
29 |
-
|
30 |
-
def wav_to_mp3(wav_uri):
|
31 |
-
AudioSegment.from_wav(wav_uri).export(wav_uri + '.mp3', format='mp3')
|
32 |
-
|
33 |
-
|
34 |
-
# sys.argv
|
35 |
-
if len(sys.argv) > 1:
|
36 |
-
src_uri = sys.argv[1]
|
37 |
-
if src_uri.endswith('.mp4'):
|
38 |
-
res = mp4_to_mp3(src_uri)
|
39 |
-
else:
|
40 |
-
res = wav_to_mp3(src_uri)
|
41 |
-
else:
|
42 |
-
print(' usage: python mp4_to_mp3.py /path/to/mp4_file ')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
python/util/to_mp3.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
|
3 |
+
from moviepy.editor import *
|
4 |
+
import sys
|
5 |
+
# print('argv[1]:' + str(sys.argv[1]))
|
6 |
+
import os
|
7 |
+
from pydub import AudioSegment
|
8 |
+
|
9 |
+
|
10 |
+
def mp4_to_mp3(mp4_uri):
|
11 |
+
# Load the mp4 file
|
12 |
+
video = VideoFileClip(mp4_uri)
|
13 |
+
|
14 |
+
# Extract audio from video
|
15 |
+
mp3_uri = mp4_uri + ".mp3"
|
16 |
+
video.audio.write_audiofile(mp3_uri)
|
17 |
+
|
18 |
+
return mp3_uri
|
19 |
+
|
20 |
+
|
21 |
+
def audio_to_wav(wav_uri, out_sample_rate=16000):
|
22 |
+
src = wav_uri
|
23 |
+
dst = wav_uri + ".wav"
|
24 |
+
if os.path.exists(dst): os.remove(dst)
|
25 |
+
|
26 |
+
# convert wav to mp3
|
27 |
+
sound = AudioSegment.from_file(src)
|
28 |
+
sound = sound.set_frame_rate(out_sample_rate)
|
29 |
+
sound.export(dst, format="wav")
|
30 |
+
|
31 |
+
print(' audio_to_wav: ', str(dst))
|
32 |
+
return dst
|
33 |
+
|
34 |
+
|
35 |
+
def wav_to_mp3(wav_uri):
|
36 |
+
mp3_uri = wav_uri + '.mp3'
|
37 |
+
AudioSegment.from_wav(wav_uri).export(mp3_uri, format='mp3')
|
38 |
+
return mp3_uri
|
39 |
+
|
40 |
+
|
41 |
+
# def wav_to_mp3_bitrate(wav_uri, bitrate):
|
42 |
+
# mp3_uri = wav_uri + '.mp3'
|
43 |
+
# AudioSegment.from_wav(wav_uri).export(mp3_uri, format='mp3', bitrate=bitrate)
|
44 |
+
# return mp3_uri
|
45 |
+
|
46 |
+
|
47 |
+
# 处理命令行
|
48 |
+
if __name__ == "__main__":
|
49 |
+
# 获取命令行参数
|
50 |
+
if len(sys.argv) < 2:
|
51 |
+
print("用法:python to_mp3.py <文件或通配符>")
|
52 |
+
sys.exit(1)
|
53 |
+
|
54 |
+
# 处理文件或通配符参数
|
55 |
+
bitrate = -1
|
56 |
+
if len(sys.argv) > 2:
|
57 |
+
bitrate = sys.argv[2]
|
58 |
+
|
59 |
+
for input_file in sys.argv[1:]:
|
60 |
+
# print("input_file:" + str(input_file))
|
61 |
+
# input_files.extend(Path().glob(input_file))
|
62 |
+
if input_file.lower().endswith(".wav"):
|
63 |
+
res = wav_to_mp3(str(input_file))
|
64 |
+
print(res)
|
65 |
+
elif input_file.lower().endswith(".mp4"):
|
66 |
+
res = mp4_to_mp3(str(input_file))
|
67 |
+
print(res)
|
68 |
+
|
69 |
+
# # 转换每个文件为 MP3 文件
|
70 |
+
# for input_file in input_files:
|
71 |
+
# if input_file.suffix.lower() == ".wav":
|
72 |
+
# res = wav_to_mp3(str(input_file))
|
73 |
+
# print(res)
|
74 |
+
# elif input_file.suffix.lower() == ".mp4":
|
75 |
+
# res = mp4_to_mp3(str(input_file))
|
76 |
+
# print(res)
|