一、获取音频
先用https://github.com/zeakhold/xmlyfetcher下载音频。
npm install -g xmlyfetcher
二、切分音频一般方法
1,利用python pydub库切割mp3 https://blog.csdn.net/u014633966/article/details/85244347
pip install pydub
并安装ffmpeg
代码:
from pydub import AudioSegment
tmp = "aaa.mp3"
audio = AudioSegment.from_mp3(tmp)
audio[35*1000:45*1000].export("out.mp3", format="mp3")
print("finish")
对于mp3音乐,可行。
对于我的音频,不可行,报错如下。
Traceback (most recent call last):
File ".\zjl周杰伦的可以切1.py", line 19, in <module>
audio = AudioSegment.from_mp3(tmp)
File "C:\Users\ASUS\Anaconda3\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "C:\Users\ASUS\Anaconda3\lib\site-packages\pydub\audio_segment.py", line 704, in from_file
p.returncode, p_err))
pydub.exceptions.CouldntDecodeError: Decoding failed. ffmpeg returned error code: 1
Output from ffmpeg/avlib:
b"ffmpeg version git-2019-12-10-e73688e Copyright (c) 2000-2019 the FFmpeg developers\r\n built with gcc 9.2.1 (GCC) 20191125\r\n configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf\r\n libavutil 56. 36.101 / 56. 36.101\r\n libavcodec 58. 64.101 / 58. 64.101\r\n libavformat 58. 35.101 / 58. 35.101\r\n libavdevice 58. 9.101 / 58. 9.101\r\n libavfilter 7. 68.100 / 7. 68.100\r\n libswscale 5. 6.100 / 5. 6.100\r\n libswresample 3. 6.100 / 3. 6.100\r\n libpostproc 55. 6.100 / 55. 6.100\r\n[mp3float @ 000001aa25db3040] Header missing\r\n Last message repeated 657 times\r\n[mp3float @ 000001aa25db3040]
big_values too big\r\n[mp3float @ 000001aa25db3040] Error while decoding MPEG audio frame.\r\n[mp3float @ 000001aa25db3040] Header missing\r\n
等等
2,mp3splt 切分
用Python分割MP3音频文件
下载mp3splt 这里
代码:
import os
os.system("mp3splt zjl.mp3 2.15 3.45 -o out.mp3")
切分周杰伦mp3音乐,可行。
我的音频,不可行,错误如下。
mp3splt 2.6.2 (09/11/14) - using libmp3splt 0.9.2
Matteo Trotta <mtrotta AT users.sourceforge.net>
Alexandru Munteanu <m AT ioalex.net>
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!
warning: output format ambiguous (@t or @n missing)
Processing file 'aaa.mp3' ...
error: no plugin matches the file 'aaa.mp3'
3,直接用ffmpeg
ffmpeg 多个音频合并 截取 拆分
输入:
ffmpeg -i aaa.mp3 -vn -acodec copy -ss 00:00:00 -t 00:01:32 output.mp3
切分周杰伦mp3音乐,可行。
我的音频,不可行,错误如下。
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'aaa.mp3':
Metadata:
major_brand : M4A
minor_version : 1
compatible_brands: isomiso2M4A mp42
encoder : Lavf58.20.100
Duration: 00:05:09.47, start: 0.000000, bitrate: 64 kb/s
Stream #0:0(und): Audio: aac (HE-AAC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 64 kb/s (default)
Metadata:
handler_name : SoundHandler
[mp3 @ 0000015137cdbc80] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Last message repeated 1 times
三、成功
使用ffmpeg,删除多余参数:
ffmpeg -i aaa.mp3 -ss 00:00:20 -t 00:01:00 output.mp3
成功。
- ffmpeg 切分工具,需要加入环境变量
- -i aaa.mp3 待切分文件
- -ss 00:00:20 从00:00:20(时分秒)开始切分
- -t 00:01:00 音频时长(而非截止位置)
- output.mp3 输出文件
截取00:00:20~00:01:20 共60秒内容
http://ffmpeg.org/ffmpeg.html 这里为ffmpeg的一些文档。
-t duration (input/output)
When used as an input option (before -i), limit the duration of data read from the input file.
When used as an output option (before an output url), stop writing the output after its duration reaches duration.
duration must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.
-to and -t are mutually exclusive and -t has priority.
Stream mapping:
Stream #0:0 -> #0:0 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, mp3, to 'output.mp3':
Metadata:
major_brand : M4A
minor_version : 1
compatible_brands: isomiso2M4A mp42
TSSE : Lavf58.35.101
Stream #0:0(und): Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc58.64.101 libmp3lame
size= 1437kB time=00:01:32.01 bitrate= 127.9kbits/s speed=50.1x
video:0kB audio:1436kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.024069%
ffmpeg -i aaa.mp3 -ss 00:00:20 -to 00:01:00 output.mp3
- -to 00:01:00 音频截止位置
截取00:00:20~00:01:00 共40秒内容
ffmpeg -loglevel quiet -i aaa.mp3 -ss 00:00:22 -to 00:04:48 output.mp3
- -loglevel quiet 沉默,不输出日志
四、编程
建议配套视频观看(见评论)
代码:
from pydub.utils import mediainfo
import os
def getLengthOfMp3(path):
# 获取mp3的时间长度
song = mediainfo(path)
time_seconds = song['duration']
time_seconds = int(float(time_seconds))
minutes = time_seconds//60
seconds = time_seconds%60
return minutes, seconds
def minusTime(path, to_delete):
# path文件位置
# to_delete 待删除的时间长度(此处<60s,一般为20s)
minutes, seconds = getLengthOfMp3(path)
if seconds>=to_delete:
# 能减完,如31s - 20s
seconds = seconds - to_delete #去掉多余的
else:
# 否则,要借位min,如5m13s - 20s
minutes -= 1
seconds = 60+seconds-to_delete
# 得4m53s
minutes,seconds = str(minutes),str(seconds) #变成字符串
minutes = minutes if len(minutes)==2 else "0"+minutes #保证两位
seconds = seconds if len(seconds)==2 else "0"+seconds
return "00:"+minutes+":"+seconds
def cutMp3(input, begin, end, output):
# 剪切mp3
# ffmpeg -i aaa.mp3 -ss 00:00:20 -to 00:01:00 output.mp3
# 本项目音频不超过1小时
# 前面22秒是广告,后面20秒也是广告
order = "ffmpeg -loglevel quiet -i " + input + " -ss "+ begin + " -to "+end +" "+output
print(order)
os.system(order)
# print("finish")
def cutAll(base="F:/music音乐/xmly喜马拉雅/tmp", out_base="F:/music音乐/xmly喜马拉雅/output"):
# 1~35集有广告
for each in os.listdir(base):
input = base + "/"+ each
output = out_base + "/"+ each
begin = "00:00:00" #开始位置
end = minusTime(input, 20) #找到截止位置,第2个参数为去除末尾广告时间
cutMp3(input, begin, end, output)
if __name__ == "__main__":
# 批量剪辑
cutAll()
print("finish..")
print("=="*30)
print()
if __name__ == "__main__2":
# 只剪辑一个
input = "aaa.mp3"
output = "output.mp3"
begin = "00:00:22" #开始位置
end = minusTime(input,20) #截止位置
cutMp3(input, begin, end, output)