添加音频压缩工具
							parent
							
								
									0bdcdd3e80
								
							
						
					
					
						commit
						590442bfe4
					
				|  | @ -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") | ||||
		Loading…
	
		Reference in New Issue