Search code examples
pythonpython-3.xyoutube-dlpraw

YouTube-dl to download and name videos from Reddit


I have been able to hack together my first script ! What it does is that it goes to a sub-reddit, gets the top submission, and then download the videos using youtube-dl. and it works !

import praw
import os

user_agent = "mybot"

r = praw.Reddit(user_agent=user_agent)

submissions = r.get_subreddit('unexpectedjihad').get_top(limit=10)
urls = []
def yt() :
    for x in submissions:
        urls.append(str(x.url))
    return urls

yt_urls = yt()

for item in yt_urls:
    print "downloading..." + " "
    os.system("youtube-dl" + " " + item)
    print "done"

What I want to do next is to get Youtube-dl to set the file name the same as the title of the reddit submission.

I get very confused thinking about how I should go about matching the title video the video file. How should I go about this ? Thank you so much


Solution

  • As per the docs, you want to pass -o to youtube-dl, E.G.:

    for item in submissions:
       os.system('youtube-dl -o {}.%(ext)s {}'.format(item.title, item.url))
    

    However, given the problems that may arise from this, embedding YDL directly might be simpler, E.G.:

    import youtube_dl
    # ... reddit stuff here ...
    
    for item in submissions:
       # see options at https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L89
       ydl_opts = {'outtmpl': item.title + '.%(ext)s'}
       with youtube_dl.YoutubeDL(ydl_opts) as ydl:
           ydl.download([item.url, ])