跑起来字节跳动音频超分开源项目versatile_audio_super_resolution


已部署在AutoDL上https://www.codewithgpu.com/i/haoheliu/versatile_audio_super_resolution/versatile_audio_super_resolution

ipynb:

复制代码
音乐 By 邓文怡 一个深圳的小姑娘

%cd /root/versatile_audio_super_resolution/运行目录

# 读取一个mp3音频文件,然后将它转换成wav格式,
# 然后每5.12秒分割为一个wav音频文件并保存到一个目录,
# 然后将文件相对路径输出到txt文件。

from pydub import AudioSegment
import os

def convert_and_split_mp3_to_wav(source_mp3, target_dir, split_interval=5120):
    # 确保目标目录存在
    os.makedirs(target_dir, exist_ok=True)
    
    # 读取并转换MP3到WAV
    audio = AudioSegment.from_mp3(source_mp3)
    base_filename = os.path.splitext(os.path.basename(source_mp3))[0]
    
    # 分割音频
    total_length = len(audio)
    splits = range(0, total_length, split_interval)
    split_files = []
    
    for i, start in enumerate(splits):
        end = start + split_interval
        split_audio = audio[start:end]
        split_filename = f"{base_filename}_part{i}.wav"
        split_path = os.path.join(target_dir, split_filename)
        split_audio.export(split_path, format="wav")
        split_files.append(split_path)
    
    # 将分割后的文件路径写入TXT文件
    with open(os.path.join(target_dir, f"{base_filename}_splits.txt"), "w") as f:
        for file in split_files:
            f.write(f"{file}\n")

# 示例用法
source_mp3 = "dwy/source.mp3"
target_dir = "dwy/splits"
convert_and_split_mp3_to_wav(source_mp3, target_dir)

!audiosr -il dwy/splits/source_splits.txt

# 从指定目录下读取wav音频文件,只需要读取一个层级,该目录没有文件夹。
# 然后将这些wav音频文件合并成一个wav音频文件并保存到指定文件夹,
# 然后将这一个wav音频文件转换成mp3音频文件并保存到指定文件夹。

from pydub import AudioSegment
import os

def merge_wav_and_convert_to_mp3(source_dir, wav_output_path, mp3_output_path):
    # 获取source_dir目录下的所有wav文件
    wav_files = [f for f in os.listdir(source_dir) if f.endswith('.wav')]
    
    # 初始化一个空的音频段
    combined = AudioSegment.empty()
    
    # 遍历所有wav文件,将它们合并
    for wav_file in wav_files:
        audio = AudioSegment.from_wav(os.path.join(source_dir, wav_file))
        combined += audio
    
    # 保存合并后的wav文件
    combined.export(wav_output_path, format="wav")
    
    # 将合并后的wav文件转换为mp3并保存
    combined.export(mp3_output_path, format="mp3")

# 示例用法
source_dir = ""
wav_output_path = "dwy/output.wav"
mp3_output_path = "dwy/output.mp3"
merge_wav_and_convert_to_mp3(source_dir, wav_output_path, mp3_output_path)

---

?MP3
分割线
?FLAC

---

# 读取一个flac音频文件,然后将它转换成wav格式,
# 然后每5.12秒分割为一个wav音频文件并保存到一个目录,
# 然后将文件相对路径输出到txt文件。
from pydub import AudioSegment
import os

def convert_and_split_flac_to_wav(source_flac, target_dir, split_interval=5120):
    # 确保目标目录存在
    os.makedirs(target_dir, exist_ok=True)
    
    # 读取并转换FLAC到WAV
    audio = AudioSegment.from_file(source_flac, format="flac")
    base_filename = os.path.splitext(os.path.basename(source_flac))[0]
    
    # 分割音频
    total_length = len(audio)
    splits = range(0, total_length, split_interval)
    split_files = []
    
    for i, start in enumerate(splits):
        end = start + split_interval
        split_audio = audio[start:end]
        split_filename = f"{base_filename}_part{i}.wav"
        split_path = os.path.join(target_dir, split_filename)
        split_audio.export(split_path, format="wav")
        split_files.append(split_path)
    
    # 将分割后的文件路径写入TXT文件
    with open(os.path.join(target_dir, f"{base_filename}_splits.txt"), "w") as f:
        for file in split_files:
            f.write(f"{file}\n")

# 示例用法
source_flac = "dwy/source.flac"
target_dir = "dwy/splits"
convert_and_split_flac_to_wav(source_flac, target_dir)

!audiosr -il dwy/splits/source_splits.txt

# 从指定目录下读取wav音频文件,只需要读取一个层级,该目录没有文件夹。 
# 然后将这些wav音频文件合并成一个wav音频文件并保存到指定文件夹,
# 然后将这一个wav音频文件转换成flac音频文件并保存到指定文件夹。
from pydub import AudioSegment
import os

def merge_wav_and_convert_to_flac(source_dir, wav_output_path, flac_output_path):
    # 获取source_dir目录下的所有wav文件
    wav_files = [f for f in os.listdir(source_dir) if f.endswith('.wav') and os.path.isfile(os.path.join(source_dir, f))]
    
    # 初始化一个空的音频段
    combined = AudioSegment.empty()
    
    # 遍历所有wav文件,将它们合并
    for wav_file in wav_files:
        audio = AudioSegment.from_wav(os.path.join(source_dir, wav_file))
        combined += audio
    
    # 保存合并后的wav文件
    combined.export(wav_output_path, format="wav")
    
    # 将合并后的wav文件转换为flac并保存
    combined.export(flac_output_path, format="flac")

# 示例用法
source_dir = "output/11_06_2024_21_05_48"
wav_output_path = "dwy/output.wav"
flac_output_path = "dwy/output.flac"
merge_wav_and_convert_to_flac(source_dir, wav_output_path, flac_output_path)

作者:likewendy
链接:https://www.codewithgpu.com/i/haoheliu/versatile_audio_super_resolution/versatile_audio_super_resolution
来源:CodeWithGpu
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
dhxhsgrx38 分钟前
PYTHON训练营DAY25
java·开发语言·python
寻丶幽风2 小时前
论文阅读笔记——双流网络
论文阅读·笔记·深度学习·视频理解·双流网络
伊织code3 小时前
PyTorch API 5 - 全分片数据并行、流水线并行、概率分布
pytorch·python·ai·api·-·5
风逸hhh3 小时前
python打卡day25@浙大疏锦行
开发语言·python
魔尔助理顾问4 小时前
Flask如何读取配置信息
python·flask·bootstrap
jc_hook5 小时前
Python 接入DeepSeek
python·大模型·deepseek
chicpopoo5 小时前
Python打卡DAY25
开发语言·python
强盛小灵通专卖员6 小时前
DL00219-基于深度学习的水稻病害检测系统含源码
人工智能·深度学习·水稻病害
crazyme_66 小时前
深入掌握 Python 切片操作:解锁数据处理的高效密码
开发语言·python
白熊1886 小时前
【计算机视觉】OpenCV实战项目:Face-Mask-Detection 项目深度解析:基于深度学习的口罩检测系统
深度学习·opencv·计算机视觉