求python将两个MP3音频文件拼接成一个MP3文件的代码

下载是示代码 enPath = "%s%s/%s"%(enDir,file,enfile) #英文文件的路径cnPath = "%s%s/%s"%(cnDir,file,enfile.replace("en_w","cn_w"))#中文文件的路径targetPath = "%s%s/%s"%(toDir,file,enfile.replace("en_w","all")) #合并文件的路径#加载MP3文件song1 = AudioSegment.from...
求python将两个MP3音频文件拼接成一个MP3文件的代码
建议用别人现成的库,不过这个库依赖于ffmpeg或avconv。在win32上不一定好安装。其实这些工作用ffmpeg的命令行也很容易实现。

from pydub import AudioSegment

sound = AudioSegment.from_mp3("/path/to/file.mp3")

# len() and slicing are in milliseconds
halfway_point = len(sound) / 2
second_half = sound[halfway_point:]

# Concatenation is just adding
second_half_3_times = second_half + second_half + second_half

# writing mp3 files is a one liner
second_half_3_times.export("/path/to/new/file.mp3", format="mp3")2014-09-08
可以使用pydub
1 网址:https://github.com/jiaaro/pydub
2 pydub需要依赖 libav或者ffmpeg
3 在mac环境下安装依赖:(二选一)

[plain] view plaincopy
brew install libav --with-libvorbis --with-sdl --with-theora
将所有依赖都安装上~~
brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis--with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools --with-fdk-aac --with-freetype --with-ffplay --with-ffplay --with-freetype --with-frei0r --with-libass --with-libbluray --with-libcaca --with-libquvi --with-libvidstab --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-openssl --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools --with-x265
4 安装pydub: pip install pydub
5 使用pydub:
下载是示代码
enPath = "%s%s/%s"%(enDir,file,enfile) #英文文件的路径cnPath = "%s%s/%s"%(cnDir,file,enfile.replace("en_w","cn_w"))#中文文件的路径targetPath = "%s%s/%s"%(toDir,file,enfile.replace("en_w","all")) #合并文件的路径#加载MP3文件song1 = AudioSegment.from_mp3(enPath)song2 = AudioSegment.from_mp3(cnPath)#取得两个MP3文件的声音分贝db1 = song1.dBFSdb2 = song2.dBFSsong1 = song1[300:] #从300ms开始截取英文MP3#调整两个MP3的声音大小,防止出现一个声音大一个声音小的情况dbplus = db1 - db2if dbplus < 0: # song1的声音更小song1+=abs(dbplus)elif dbplus > 0: #song2的声音更小song2+=abs(dbplus)#拼接两个音频文件song = song1 + song2#导出音频文件song.export(targetPath, format="mp3") #导出为MP3格式2016-01-02
mengvlog 阅读 11 次 更新于 2025-07-19 16:13:08 我来答关注问题0
  •  湖北倍领科技 python怎样同时播放两个音频?winsound不行啊,除了pygame有别的办法么

    首先,导入必要的库:python import pygame import time 然后,初始化mixer模块:python pygame.mixer.init()接下来,加载并播放第一个音频文件:python s = pygame.mixer.Sound('./mscs/hhmj.mp3')s.play()同样地,加载并播放第二个音频文件:python pygame.mixer.music.load('./mscs/wzx.mp3')py...

  • 1 网址:https://github.com/jiaaro/pydub 2 pydub需要依赖 libav或者ffmpeg 3 在mac环境下安装依赖:(二选一)[plain] view plaincopy brew install libav --with-libvorbis --with-sdl --with-theora 将所有依赖都安装上~~brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype -...

  •  翡希信息咨询 如何让Python朗读在线音频和本地音频

    朗读本地音频方法一:使用pygame模块: 安装模块:安装pygame模块。 读取音频:使用pygame的mixer功能读取并播放本地MP3文件。这种方法简单直接,适用于大多数本地音频文件。方法二:使用pydub模块: 安装模块:安装pydub模块,以及它依赖的ffmpeg或libav。 读取音频:使用pydub加载本地MP3文件,并播放。与pygame...

  •  文暄生活科普 如何让Python朗读在线音频和本地音频

    1. 使用pygame法 这种方法与第二种方法类似,只是不需要获取网络的音频,直接读取本地音频即可。在示例中,您需要将your_file_path.mp3替换为您本地MP3文件的路径,然后运行脚本,播放指定的MP3文件。2. 使用pydub法 pydub的体积可能会比pygame小一些,打包时更省力。以下是一个样例代码,供您参考。首先...

  •  翡希信息咨询 利用Python将PDF文档转为MP3音频

    利用Python将PDF文档转为MP3音频,可以通过以下步骤实现:安装必要的Python库:pdfplumber:用于处理PDF文档中的文本信息。pyttsx3:用于将文本转换为语音,并导出为MP3文件。读取PDF文档中的文本:使用pdfplumber库读取PDF文档,并提取出其中的文本内容。将文本转换为语音:使用pyttsx3库将提取出的文本内容转换为...

檬味博客在线解答立即免费咨询

Python相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部