跑起来字节跳动音频超分开源项目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
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
Kratzdisteln5 小时前
【MVCD】PPT提纲汇总
经验分享·python
一个无名的炼丹师5 小时前
GraphRAG深度解析:从原理到实战,重塑RAG检索增强生成的未来
人工智能·python·rag
AI街潜水的八角6 小时前
基于Pytorch深度学习神经网络MNIST手写数字识别系统源码(带界面和手写画板)
pytorch·深度学习·神经网络
用户8356290780516 小时前
用Python轻松管理Word页脚:批量处理与多节文档技巧
后端·python
进击的松鼠6 小时前
LangChain 实战 | 快速搭建 Python 开发环境
python·langchain·llm
小北方城市网6 小时前
第1课:架构设计核心认知|从0建立架构思维(架构系列入门课)
大数据·网络·数据结构·python·架构·数据库架构
我的offer在哪里6 小时前
Hugging Face:让大模型触手可及的魔法工厂
人工智能·python·语言模型·开源·ai编程
汤姆yu7 小时前
基于python大数据的协同过滤音乐推荐系统
大数据·开发语言·python
爱学习的小道长7 小时前
Python Emoji库的使用教程
开发语言·python
Data_agent7 小时前
Cssbuy 模式淘宝 / 1688 代购系统南美市场搭建指南
大数据·python