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

时间:2020-03-16 12:12:07

标签: python download reddit moviepy

我正在尝试编写一个程序,可以从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')

1 个答案:

答案 0 :(得分:1)

您可以尝试使用现有的开源工具之一来实现此目的,例如youtube-dl(其下载量远超过其名称所暗示的)。 previous SO thread已经介绍了如何在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/")

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

相关问题