Python-从youtube下载视频并将其转换为mp3

时间:2016-08-08 09:59:40

标签: python pafy

此代码下载视频并将其转换为mp3文件。但是,mp3音频将比普通视频长2倍。我该如何解决这个问题?

import pafy
import os
import moviepy.editor as mp


print "[+] Welcome to Youtube downloader."
download_url = raw_input("URL :")

video = pafy.new(download_url)
best = video.streams
file_name = video.streams[0]
print file_name

directory = "downloaded-music"
if not os.path.exists(directory):
    os.makedirs(directory)
x = file_name.download(filepath = directory)

clip = mp.VideoFileClip(x)
print clip.size
clip.audio.write_audiofile(x + ".mp3")

os.remove(x)

1 个答案:

答案 0 :(得分:0)

它的价值" clip.size"比实际的大两倍,或者它是文件的真正长度?