音樂(lè)視頻轉(zhuǎn)音頻,分割成單個(gè)文件

1. 從youbube上下載音樂(lè)視頻,MP4格式

推薦:

https://github.com/ErikZhou/YouPy

2. MP4格式轉(zhuǎn)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系統(tǒng)),根據(jù)靜音間隔來(lái)分割歌曲

import subprocess as sp

import sys

import numpy

FFMPEG_BIN = "ffmpeg"

print ('ASplit.py <src.mp3> <silence duration in seconds> <threshold amplitude 0.0 .. 1.0>')

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()


輸出結(jié)果:(文件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文件抽取單個(gè)MP3格式的歌曲

運(yùn)行1_1.mp3-out.sh文件,最終結(jié)果如下:



5. 完整工作流啟動(dòng)代碼

輸入:同級(jí)目錄下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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容