diff --git a/audio_compress.py b/audio_compress.py new file mode 100755 index 0000000..08d070b --- /dev/null +++ b/audio_compress.py @@ -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") \ No newline at end of file diff --git a/音频压缩.bat b/音频压缩.bat new file mode 100644 index 0000000..2ef0c87 --- /dev/null +++ b/音频压缩.bat @@ -0,0 +1,5 @@ +:check +python audio_compress.py ./bgm +pause + +goto check \ No newline at end of file