File size: 5,342 Bytes
cf2f35c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
音频文件转换和片段提取工具
将MP3文件转换为WAV格式,并提取指定时间段的音频片段
"""

import os
import subprocess
from pathlib import Path

def convert_mp3_to_wav_and_extract(input_file, start_time, end_time, output_dir=None):
    """
    将MP3文件转换为WAV格式,并提取指定时间段的音频片段
    
    Args:
        input_file (str): 输入的MP3文件路径
        start_time (float): 开始时间(秒)
        end_time (float): 结束时间(秒)
        output_dir (str): 输出目录,如果为None则使用输入文件所在目录
    
    Returns:
        bool: 操作是否成功
    """
    try:
        # 检查输入文件是否存在
        if not os.path.exists(input_file):
            print(f"❌ 错误:输入文件不存在: {input_file}")
            return False
        
        # 设置输出目录
        if output_dir is None:
            output_dir = os.path.dirname(input_file)
        
        # 确保输出目录存在
        Path(output_dir).mkdir(parents=True, exist_ok=True)
        
        # 生成输出文件名
        input_name = Path(input_file).stem
        output_wav = os.path.join(output_dir, f"{input_name}.wav")
        output_segment = os.path.join(output_dir, f"{input_name}_segment_{start_time}s_to_{end_time}s.wav")
        
        print(f"🎵 开始处理音频文件: {input_file}")
        print(f"📁 输出目录: {output_dir}")
        
        # 步骤1:将MP3转换为WAV格式
        print(f"\n🔄 步骤1: 将MP3转换为WAV格式")
        convert_cmd = [
            'ffmpeg',
            '-y',  # 覆盖输出文件
            '-i', input_file,  # 输入文件
            '-ar', '16000',  # 采样率16kHz
            '-ac', '1',  # 单声道
            '-c:a', 'pcm_s16le',  # 16位PCM编码
            output_wav  # 输出文件
        ]
        
        print(f"执行命令: {' '.join(convert_cmd)}")
        result = subprocess.run(convert_cmd, capture_output=True, text=True)
        
        if result.returncode != 0:
            print(f"❌ MP3转WAV失败: {result.stderr}")
            return False
        
        print(f"✅ MP3转WAV成功: {output_wav}")
        
        # 步骤2:提取音频片段
        print(f"\n🔄 步骤2: 提取音频片段 ({start_time}s - {end_time}s)")
        duration = end_time - start_time
        
        extract_cmd = [
            'ffmpeg',
            '-y',  # 覆盖输出文件
            '-i', output_wav,  # 输入WAV文件
            '-ss', str(start_time),  # 开始时间
            '-t', str(duration),  # 持续时间
            '-c', 'copy',  # 直接复制,不重新编码
            output_segment  # 输出片段文件
        ]
        
        print(f"执行命令: {' '.join(extract_cmd)}")
        result = subprocess.run(extract_cmd, capture_output=True, text=True)
        
        if result.returncode != 0:
            print(f"❌ 音频片段提取失败: {result.stderr}")
            return False
        
        print(f"✅ 音频片段提取成功: {output_segment}")
        
        # 显示文件信息
        print(f"\n📊 文件信息:")
        print(f"原始MP3文件: {input_file}")
        print(f"转换后的WAV文件: {output_wav}")
        print(f"提取的音频片段: {output_segment}")
        print(f"片段时长: {duration:.1f}秒")
        
        # 检查输出文件大小
        if os.path.exists(output_wav):
            wav_size = os.path.getsize(output_wav) / 1024  # KB
            print(f"WAV文件大小: {wav_size:.1f} KB")
        
        if os.path.exists(output_segment):
            segment_size = os.path.getsize(output_segment) / 1024  # KB
            print(f"片段文件大小: {segment_size:.1f} KB")
        
        return True
        
    except Exception as e:
        print(f"❌ 处理过程中出现错误: {str(e)}")
        return False

def main():
    """主函数"""
    print("🎵 音频文件转换和片段提取工具")
    print("=" * 50)
    
    # 检查ffmpeg是否安装
    try:
        subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
        print("✅ 检测到ffmpeg")
    except (subprocess.CalledProcessError, FileNotFoundError):
        print("❌ 错误:未找到ffmpeg,请先安装ffmpeg")
        print("Ubuntu/Debian: sudo apt install ffmpeg")
        print("CentOS/RHEL: sudo yum install ffmpeg")
        print("macOS: brew install ffmpeg")
        return
    
    # 设置文件路径和时间参数
    input_file = "/home/t2vg-a100-G4-42/v-shuyuantu/StableAvatar/example_case/case-3/ssvid.net--Wellerman-Female-Cover-LYRICS-Sea-Shanty.mp3"
    start_time = 1.9  # 开始时间(秒)
    end_time = 7.1    # 结束时间(秒)
    
    print(f"📁 输入文件: {input_file}")
    print(f"⏰ 提取时间段: {start_time}s - {end_time}s")
    print(f"⏱️ 片段时长: {end_time - start_time:.1f}秒")
    
    # 执行转换和提取
    success = convert_mp3_to_wav_and_extract(input_file, start_time, end_time)
    
    if success:
        print(f"\n🎉 所有操作完成!")
        print(f"输出文件保存在: {os.path.dirname(input_file)}")
    else:
        print(f"\n❌ 操作失败,请检查错误信息")

if __name__ == "__main__":
    main()