温馨提示:本文翻译自stackoverflow.com,查看原文请点击:download - Is there a fast way to combine an mp3 file with an mp4 file in python?
download python reddit moviepy

download - 有没有一种快速的方法可以将mp3文件与python中的mp4文件合并在一起?

发布于 2020-05-31 09:43:59

我正在尝试编写一个程序,可以从Reddit帖子中下载视频。我相信Reddit会分别存储每个帖子的音频和视频,因此我目前正在下载mp3和mp4,然后将它们组合成一个最终的视频文件。我对音频或视频文件或它们的存储方式不是很熟悉,但是我认为将两者结合起来可以快速进行计算。

但是,合并部分非常慢,我想知道是否有更快的方法将无声视频片段与音频文件合并并将其写入驱动器?

我目前正在使用moviepy库进行合并。

def download_video(data_url,current_post,subreddit):
    #Get the audio url of Reddit video
    audioURL = data_url + "/audio"
    #Get the soundless video url of reddit video
    videoURL = str(current_post).split("'fallback_url': '")[1].split("'")[0]
    #Get the title of the post
    postname = (current_post['title'])

    #Download the two files as mp4 and mp3
    urllib.request.urlretrieve(videoURL, subreddit + '/video_name.mp4')
    urllib.request.urlretrieve(audioURL, subreddit + '/audio.mp3')

    #Combine the mp3 and mp4
    videoName = str(subreddit + "/" + get_valid_filename(current_post['title'])) +".mp4"
    video = mpe.VideoFileClip(subreddit + '/video_name.mp4')
    video.write_videofile(videoName, audio=subreddit + "/audio.mp3")
    #Remove video file with no audio
    del video
    os.remove(subreddit + '/video_name.mp4')

查看更多

提问者
Peter
被浏览
22
Oliver.R 2020-03-16 20:36

您可以尝试使用现有的开源工具之一来实现此目的,例如youtube-dl(下载量远不如其名称所示)。一个以前的SO线已经涵盖了如何从Python中做到这一点,我刚刚测试了两个线程链接和v.redd.it链接,把它与要么没有问题的工作。

import youtube_dl

ydl = youtube_dl.YoutubeDL()
with ydl:
    ydl.extract_info("https://www.reddit.com/r/bouldering/comments/fjgmo7/one_of_my_favorite_boulders_from_my_gym_back_home/")

如果这样做可以提高性能,但是您不希望使用该库,则可以检查其来源,以了解他们如何进行视频和音频合并。