添加音频压缩工具

main
程祎 2023-11-28 10:42:00 +08:00
parent 0bdcdd3e80
commit 590442bfe4
2 changed files with 52 additions and 0 deletions

47
audio_compress.py Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/python
# coding=utf-8
#音频文件压缩
from pydub import AudioSegment
import os, sys
total = 0
def compress(path):
global total
if path.find(" ") != -1:
print("文件名包含空格, 请处理: " + path)
return
arr = os.path.splitext(path)
format = arr[1].replace(".", "")
size_kb_pre = os.path.getsize(path)/1024
audio = None
changed = False
if format == "ogg":
audio = AudioSegment.from_ogg(path)
elif format == "mp3":
audio = AudioSegment.from_mp3(path)
if audio:
if audio.channels >= 2:
audio = audio.set_channels(1)
changed = True
if audio.frame_rate > 32000:
audio = audio.set_frame_rate(32000)
changed = True
if changed:
audio.export(path, format=format,tags={'artist': 'AppLeU0', 'album': path})
size_kb_later = os.path.getsize(path)/1024
total = total + (size_kb_pre - size_kb_later)
print("压缩: " + str(size_kb_pre) + "KB -> " + str(size_kb_later) + "KB 总计: " + str(size_kb_pre - size_kb_later) + "KB")
if __name__ == '__main__':
for root , dirs, files in os.walk(sys.argv[1]):
for name in files:
if name.endswith('.mp3') or name.endswith('.ogg'):
compress(os.path.join(root, name))
print(f"压缩完成, 总计节省: {total}KB")

5
音频压缩.bat Normal file
View File

@ -0,0 +1,5 @@
:check
python audio_compress.py ./bgm
pause
goto check