音乐视频转音频,分割成单个文件

1. 从youbube上下载音乐视频,MP4格式

推荐:

https://github.com/ErikZhou/YouPy

2. MP4格式转MP3格式

方法1:


方法2

import os

import sys

from moviepy.editor import *

def to_mp3(filename):

    mp3file = filename[:-3] + 'mp3'

    print(mp3file)

    video = VideoFileClip(os.path.join("./","",filename))

    video.audio.write_audiofile(os.path.join("./","",mp3file))

def usage():

    print('python mp4-to-mp3.py filename')

def main():

    filename = sys.argv[1]

    print(filename)

    to_mp3(filename)

if __name__ == "__main__":

    usage()

    main()


3. 利用ffmpeg分割MP3文件(Mac系统),根据静音间隔来分割歌曲

import subprocess as sp

import sys

import numpy

FFMPEG_BIN = "ffmpeg"

print ('ASplit.py ')

print('eg. python asplit.py 1_1.mp3 3 0.1')

src = sys.argv[1]

dur = float(sys.argv[2])

thr = int(float(sys.argv[3]) * 65535)

f = open('%s-out.sh' % src, 'w+')

tmprate = 22050

len2 = dur * tmprate

buflen = int(len2    * 2)

#            t * rate * 16 bits

oarr = numpy.arange(1, dtype='int16')

# just a dummy array for the first chunk

command = [ FFMPEG_BIN,

        '-i', src,

        '-f', 's16le',

        '-acodec', 'pcm_s16le',

        '-ar', str(tmprate), # ouput sampling rate

        '-ac', '1', # '1' for mono

        '-']        # - output to stdout

pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10**8)

tf = True

pos = 0

opos = 0

part = 0

while tf :

    raw = pipe.stdout.read(buflen)

    if raw == '' :

        tf = False

        break

    try:

        arr = numpy.fromstring(raw, dtype = "int16")

        rng = numpy.concatenate([oarr, arr])

        mx = numpy.amax(rng)

    except ValueError:  #raised if `y` is empty.

        print('ValueError')

        break

        #pass

    if mx <= thr :

        # the peak in this range is less than the threshold value

        trng = (rng <= thr) * 1

        # effectively a pass filter with all samples <= thr set to 0 and > thr set to 1

        sm = numpy.sum(trng)

        # i.e. simply (naively) check how many 1's there were

        print('sm={}, len2={}'.format(sm, len2))

        apos1 = pos + dur * 0.5


        if sm >= len2 and (apos1 - opos) > 20:

            part += 1

            apos = pos + dur * 0.5

            print (mx, sm, len2, apos)

            #f.write('ffmpeg -i "%s" -ss %f -to %f -c copy -y "%s-p%04d.mp3"\r\n' % (src, opos, apos, src, part))

            f.write('ffmpeg -i {} -ss {} -to {} -c copy -y {}p{:02n}.mp3 &\r\n'.format(src, opos, apos, src[:2], part))

            opos = apos

    pos += dur

    oarr = arr

part += 1   

#f.write('ffmpeg -i "%s" -ss %f -to %f -c copy -y "%s-p%04d.mp3"\r\n' % (src, opos, pos, src, part))

#f.write('ffmpeg -i {} -ss {} -to {} -c copy -y {}p{:04n}.mp3 \r\n'.format(src, opos, apos, '', part))

f.close()


输出结果:(文件1_1.mp3-out.sh)

ffmpeg -i 1_1.mp3 -ss 0 -to 349.5 -c copy -y 1_p01.mp3 &

ffmpeg -i 1_1.mp3 -ss 349.5 -to 685.5 -c copy -y 1_p02.mp3 &

ffmpeg -i 1_1.mp3 -ss 685.5 -to 934.5 -c copy -y 1_p03.mp3 &

ffmpeg -i 1_1.mp3 -ss 934.5 -to 1174.5 -c copy -y 1_p04.mp3 &

ffmpeg -i 1_1.mp3 -ss 1174.5 -to 1441.5 -c copy -y 1_p05.mp3 &

ffmpeg -i 1_1.mp3 -ss 1441.5 -to 1657.5 -c copy -y 1_p06.mp3 &

ffmpeg -i 1_1.mp3 -ss 1657.5 -to 1921.5 -c copy -y 1_p07.mp3 &

ffmpeg -i 1_1.mp3 -ss 1921.5 -to 2665.5 -c copy -y 1_p08.mp3 &

ffmpeg -i 1_1.mp3 -ss 2665.5 -to 2926.5 -c copy -y 1_p09.mp3 &

ffmpeg -i 1_1.mp3 -ss 2926.5 -to 3346.5 -c copy -y 1_p10.mp3 &

ffmpeg -i 1_1.mp3 -ss 3346.5 -to 3601.5 -c copy -y 1_p11.mp3 &

ffmpeg -i 1_1.mp3 -ss 3601.5 -to 3877.5 -c copy -y 1_p12.mp3 &

ffmpeg -i 1_1.mp3 -ss 3877.5 -to 4144.5 -c copy -y 1_p13.mp3 &

4. 从大MP3文件抽取单个MP3格式的歌曲

运行1_1.mp3-out.sh文件,最终结果如下:



5. 完整工作流启动代码

输入:同级目录下MP4文件列表

输出:分割好的MP3文件

import os

import sys

import glob

import os.path

from os import path

import mp4_to_mp3

def to_sh(filename):

    mp3file = filename[:-3] + 'mp3'

    if path.exists(mp3file):

        print('exists:'+mp3file)

        return mp3file

    mp4_to_mp3.to_mp3(filename)

    return mp3file

mylist = [f for f in glob.glob("*.mp4")]

#print(mylist)

for c in mylist:

    print(c)

    mp3file = to_sh(c)


    cmd = 'python asplit.py ' + mp3file +' 3 0.1'

    os.system(cmd)

    shfile = mp3file + '-out.sh'

    cmd = 'chmod +x ' + shfile

    os.system(cmd)

    cmd = './' + shfile

    os.system(cmd)

你可能感兴趣的:(音乐视频转音频,分割成单个文件)