跑起来字节跳动音频超分开源项目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
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
Wen.py.java11 分钟前
Python商务数据分析知识专栏(六)——Python数据分析的应用④Python数据分析实训
人工智能·python·数据分析
不死鸟.亚历山大.狼崽子13 分钟前
python库(2):Passlib库
开发语言·python
Zucker n29 分钟前
学会python——用python制作一个登录和注册窗口(python实例十八)
开发语言·python
pictoexcel39 分钟前
名片批量转换为结构化Excel注意的事项
人工智能·深度学习·神经网络·机器学习·ocr·excel
Eiceblue43 分钟前
Python 插入、替换、提取、或删除Excel中的图片
开发语言·vscode·python·pycharm·excel
神奇夜光杯1 小时前
Python酷库之旅-第三方库Pandas(003)
开发语言·ide·python·pandas·基础知识·学习和成长·标准库及第三方库
Leon哉1 小时前
PyCharm中如何将某个文件设置为默认运行文件
ide·python·pycharm
wenpingyang1 小时前
python学习2-数据结构与算法-链表
python·学习·链表
孤寒者1 小时前
(三十一)Flask之wtforms库【剖析源码下篇】
后端·python·flask·源码剖析·wtforms
milong5212 小时前
Flask自定义命令
后端·python·flask