B06.有意思的小东西 -切分音频mp3,过滤不感兴趣的部分

一、获取音频

先用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)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
帮我优化以下代码<style> * { box-sizing: border-box; } .row::after { content: ""; clear: both; display: table; } [class*="col-"] { float: left; padding: 15px; } html { font-family: "Lucida Sans", sans-serif; } .header { background-color: #0603F7; color: #ffffff; padding: 3px; } .menu ul { list-style-type: none; margin: 0; padding: 0; } .menu li { padding: 8px; margin-bottom: 7px; background-color: #33b5e5; color: #ffffff; box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); } .menu li:hover { background-color: #F59B06; } .aside { background-color: #33b5e5; padding: 15px; color: #ffffff; text-align: center; font-size: 14px; box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); } .footer { background-color: #0099cc; color: #ffffff; text-align: center; font-size: 12px; padding: 15px; } /* For mobile phones: */ [class*="col-"] { width: 100%; } @media only screen and (min-width: 600px) { /*小屏幕 */ .col-s-1 {width: 8.33%;} .col-s-2 {width: 16.66%;} .col-s-3 {width: 100%;} .col-s-4 {width: 33.33%;} .col-s-5 {width: 41.66%;} .col-s-6 {width: 75%;} .col-s-7 {width: 58.33%;} .col-s-8 {width: 66.66%;} .col-s-9 {width: 100%;} .col-s-10 {width: 83.33%;} .col-s-11 {width: 91.66%;} .col-s-12 {width: 100%;} } @media only screen and (min-width: 768px) { /*大屏幕*/ .col-1 {width: 8.33%;} .col-2 {width: 16.66%;} .col-3 {width: 100%;} .col-4 {width: 33.33%;} .col-5 {width: 41.66%;} .col-6 {width: 100%;} .col-7 {width: 58.33%;} .col-8 {width: 66.66%;} .col-9 {width: 75%;} .col-10 {width: 83.33%;} .col-11 {width: 91.66%;} .col-12 {width: 100%;} } </style>
05-30
首先,这段代码已经非常简洁了,但还是有一些优化空间: 1. 将重复的样式提取出来,避免重复书写。例如,可以将 `.menu li` 和 `.aside` 中的 `background-color`,`box-shadow`,`color` 和 `padding` 提取出来,作为一个公共的样式类,再在需要使用的地方加上该样式类即可。 2. 可以考虑使用 CSS 预处理器来编写样式,例如 Sass 或 Less,可以更加简洁、易读、易维护。 3. 可以将媒体查询中的重复代码提取出来,避免重复书写。例如,可以将 `.col-s-*` 和 `.col-*` 中的宽度设置提取出来,再在媒体查询中加上相应的样式即可。 4. 对于类名中的数字,可以考虑使用循环语句生成,避免重复书写。 以下是优化后的代码: ``` <style> * { box-sizing: border-box; } .row::after { content: ""; clear: both; display: table; } .common-style { background-color: #33b5e5; color: #ffffff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24); padding: 15px; } .header { background-color: #0603F7; color: #ffffff; padding: 3px; } .menu ul { list-style-type: none; margin: 0; padding: 0; } .menu li { padding: 8px; margin-bottom: 7px; box-shadow: none; } .menu li:hover { background-color: #F59B06; } .aside { text-align: center; font-size: 14px; } .aside-common { width: 100%; } .footer { background-color: #0099cc; color: #ffffff; text-align: center; font-size: 12px; padding: 15px; } /* For mobile phones: */ [class*="col-"] { width: 100%; } @for $i from 1 through 12 { $width: percentage($i / 12); .col-s-#{$i} { width: $width; } @media only screen and (min-width: 600px) { .col-#{$i} { width: $width; } } } .col-s-3, .col-s-9 { width: 100%; } @media only screen and (min-width: 768px) { .col-s-6 { width: 75%; } .col-s-7 { width: 58.33%; } .col-s-8 { width: 66.66%; } .col-s-10 { width: 83.33%; } .col-s-11 { width: 91.66%; } .col-3 { width: 100%; } .col-6 { width: 100%; } .col-9 { width: 75%; } } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值