Skip to content

Instantly share code, notes, and snippets.

@DarkPointer
Created September 17, 2019 21:48
Show Gist options
  • Save DarkPointer/228a22f717e5809647852299c290a3a9 to your computer and use it in GitHub Desktop.
Save DarkPointer/228a22f717e5809647852299c290a3a9 to your computer and use it in GitHub Desktop.
Parse, download and merge reddit video and audio
import re
import requests
import tempfile
import subprocess
import urllib.request
def parseRedditVideo(link: str, outPath: str):
response = requests.get(url=link, headers = {'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'})
json = response.json()
isVideo = json[0]['data']['children'][0]['data']['is_video']
if not isVideo: return
mpdListLink = json[0]['data']['children'][0]['data']['media']['reddit_video']['dash_url']
mpdResponse = requests.get(url=mpdListLink, headers = {'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'})
mpdXMLData = mpdResponse.text
baseLink = json[0]['data']['children'][0]['data']['url']
reSearchData = re.findall('<BaseURL>(.*?)</BaseURL>', mpdXMLData)
highestVideoQualityFullLink = reSearchData[0]
audioPartialLink = reSearchData[-1]
highestVideoQualityFullLink = baseLink + '/' + highestVideoQualityFullLink
audioFullLink = baseLink + '/' + audioPartialLink
tempDir = tempfile.gettempdir()
tempVideoFilepath = tempDir + '\\' + next(tempfile._get_candidate_names())
tempAudioFilepath = tempDir + '\\' + next(tempfile._get_candidate_names())
urllib.request.urlretrieve(highestVideoQualityFullLink, tempVideoFilepath)
urllib.request.urlretrieve(audioFullLink, tempAudioFilepath)
ffmpegJoiningCommand = f"ffmpeg -i \"{tempVideoFilepath}\" -i \"{tempAudioFilepath}\" -c copy \"{outPath}\""
subprocess.call(ffmpegJoiningCommand, shell=True)
def main():
#Example
link = "https://www.reddit.com/r/Animemes/comments/d5ga6a/sign_memes_are_now_banned/.json"
parseRedditVideo(link,"G:\\out.mkv")
main()
@LazieKat
Copy link

Very nice

@k3rn3ld3v
Copy link

Thanks. Works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment