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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| import subprocess import os import json from pathlib import Path
class AudioPreprocessor: """TTS语音复刻音频预处理器""" def __init__(self, ffmpeg_path='ffmpeg'): self.ffmpeg_path = ffmpeg_path def detect_id3_tags(self, audio_path): """检测音频文件中的ID3标签""" cmd = [ self.ffmpeg_path, '-i', audio_path, '-f', 'null', '-' ] result = subprocess.run(cmd, capture_output=True, text=True) tags_info = { 'has_id3v1': 'ID3v1' in result.stderr, 'has_id3v2': 'ID3v2' in result.stderr, 'has_apic': 'APIC' in result.stderr, 'has_comm': 'COMM' in result.stderr, 'metadata_size': self._extract_metadata_size(result.stderr) } return tags_info def strip_metadata(self, input_path, output_path): """剥离所有ID3元数据""" cmd = [ self.ffmpeg_path, '-i', input_path, '-map_metadata', '-1', '-id3v2_version', '0', '-write_id3v1', '0', '-y', output_path ] subprocess.run(cmd, check=True, capture_output=True) return output_path def reencode_audio(self, input_path, output_path, sample_rate=44100, channels=2): """重编码音频为标准格式""" cmd = [ self.ffmpeg_path, '-i', input_path, '-ar', str(sample_rate), '-ac', str(channels), '-sample_fmt', 's16', '-codec:a', 'libmp3lame', '-b:a', '192k', '-y', output_path ] subprocess.run(cmd, check=True, capture_output=True) return output_path def validate_audio(self, audio_path): """验证处理后的音频文件""" cmd = [ self.ffmpeg_path, '-i', audio_path, '-f', 'null', '-' ] result = subprocess.run(cmd, capture_output=True, text=True) validation = { 'is_valid': result.returncode == 0, 'has_metadata': 'Metadata:' in result.stderr, 'format': self._extract_format(result.stderr), 'duration': self._extract_duration(result.stderr) } return validation def process(self, input_path, output_dir=None): """完整的预处理流程""" input_path = Path(input_path) if output_dir is None: output_dir = input_path.parent output_path = Path(output_dir) / f"{input_path.stem}_clean.mp3" print(f"[1/4] 检测ID3标签: {input_path.name}") tags_info = self.detect_id3_tags(str(input_path)) print(f" 发现标签: {json.dumps(tags_info, indent=2)}") print(f"[2/4] 剥离元数据...") temp_path = str(output_path) + '.temp.mp3' self.strip_metadata(str(input_path), temp_path) print(f"[3/4] 重编码音频...") self.reencode_audio(temp_path, str(output_path)) print(f"[4/4] 验证处理结果...") validation = self.validate_audio(str(output_path)) if os.path.exists(temp_path): os.remove(temp_path) print(f" 验证结果: {json.dumps(validation, indent=2)}") return { 'input': str(input_path), 'output': str(output_path), 'original_tags': tags_info, 'validation': validation } def _extract_metadata_size(self, stderr_output): """从ffmpeg输出中提取元数据大小""" import re match = re.search(r'Metadata:\s*size:\s*(\d+)', stderr_output) return int(match.group(1)) if match else 0 def _extract_format(self, stderr_output): """从ffmpeg输出中提取音频格式""" import re match = re.search(r'Audio:\s+(\w+)', stderr_output) return match.group(1) if match else 'unknown' def _extract_duration(self, stderr_output): """从ffmpeg输出中提取音频时长""" import re match = re.search(r'Duration:\s+(\d+:\d+:\d+\.\d+)', stderr_output) return match.group(1) if match else 'unknown'
if __name__ == '__main__': preprocessor = AudioPreprocessor() result = preprocessor.process('input_audio.mp3') print(f"\n处理完成: {result['output']}") import glob for mp3_file in glob.glob('raw_audio/*.mp3'): result = preprocessor.process(mp3_file, 'processed_audio/')
|