2

I am trying to write a program that can download videos from Reddit posts. I believe that Reddit stores the audio and video for each post separately, so I am currently downloading the mp3 and the mp4 and then combining them to make a final video file. I am not very familiar with audio or video files or how they are stored, but I thought that combining the two would be quick to compute.

However, the combining part is very slow and I was wondering if there is a faster way of combining a soundless video clip with an audio file and writing it to my drive?

I am currently using the moviepy library for the combining.

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
  • 848
  • 8
  • 14

1 Answers1

1

You could try using one of the existing open-source tools that achieves this, such as youtube-dl (which downloads much more than what its name suggests). A previous SO thread has already covered how to do this from within Python, and I've just tested it on both thread links and v.redd.it links and had it work with no issues with either.

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/")

If this has improved performance but you would prefer not to use the library, you could check their source to see how they're doing their video and audio combining.

Oliver.R
  • 1,282
  • 7
  • 17
  • I've tried this but I get the error: 'WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.' – Peter Mar 16 '20 at 12:57
  • Install [ffmpeg](https://www.ffmpeg.org/) (or avconv, but I had ffmpeg for my test so I know that works) – Oliver.R Mar 16 '20 at 13:00
  • I already have ffmpeg installed in my python libraries 'pip install ffmpeg-python' so I'm quite confused. – Peter Mar 16 '20 at 13:02
  • 1
    I believe youtube-dl calls ffmpeg directly, so you need the binaries (ie ffmpeg installed straight from the website). It doesn't require ffmpeg-python (I don't have that installed and it isn't an issue). – Oliver.R Mar 16 '20 at 13:12
  • I've downloaded the folder from the website, unzipped it and put it in the same folder as my python code, but it still isn't working. – Peter Mar 16 '20 at 13:17
  • You need the `.bin` files of ffmpeg to be on your Windows path - see [this previous SO thread](https://stackoverflow.com/a/41822439/10993299) – Oliver.R Mar 16 '20 at 13:21