音频的生成:
此处使用的是:Pysynth, github地址:https://github.com/mdoege/PySynth,网上的其他教程安装有问题,根据官网步骤即可。
推荐的另一种音频生成工具类是:https://github.com/Rainbow-Dreamer/musicpy
install:
git clone git@github.com:mdoege/PySynth.git cd PySynth python3 setup.py install
其对应的音符如下(低音使用数字2,中音3,高音4):
1(do): c3 2(re): d3 3(mi): e3 4(fa): f3 5(sol): g3 6(la): a3 7(si): b3
示例详见GitHub官网内容
音频的混音:
需用到 ffmpeg,其官网为:https://ffmpeg.org/ ,widow版本的直接在官网下载,需要配置环境变量
linux的安装:
sudo yum install epel-release sudo rpm -v --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm sudo yum install ffmpeg ffmpeg-devel
检查安装是否成功
ffmpeg -version
使用到的第三方工具类是pydub,直接pip安装即可,
pip install pydub
示例:
from pydub import AudioSegment import sys import math org = sys.argv[1] # print(sys.argv) print("====begin=====") print(sys.argv[1]) waterMark = sys.argv[2] outpout = sys.argv[3] # overlay # position:覆盖起始位置(毫秒) # loop:是否循环覆盖(true/false) # times:重复覆盖次数(默认1) # gain_during_overlay:调整被覆盖音频的音量(eg,-6.0) if ".wav" in org: sound1 = AudioSegment.from_wav(org) else: sound1 = AudioSegment.from_mp3(org) if ".wav" in waterMark: sound2 = AudioSegment.from_wav(waterMark) else: sound2 = AudioSegment.from_mp3(waterMark) times = int(math.ceil(len(sound1) / 10000)) output = sound1 print(times) for i in range(times): output = output.overlay(sound2, position=(10000*i)) output.export(outpout, "mp3") print("====end=====")