Search code examples
pythonregexyoutube-dl

Python script only downloads the first video from the url list


I made a python script to download some videos using youtube-dl. When I run the script , it downloads the first video and then keeps printing video already downloaded for all other videos.

Here is the script:

import re , os
formula = re.compile(r'https.+')

os.chdir('/home/ubuntu/Desktop/TO MASTER THIS VACATION')
urls = []
with open('Python necessary videos.txt' , 'r') as f:
    for line in f:
        mo = formula.findall(line)
        if mo:
            urls.append(mo[0])
cmd = 'youtube-dl -f 22 '
for i in urls:
    print("Video url : "+str(i))
    cmd = cmd + str(i)
    os.system(cmd)

What am I doing wrong??

P.S.

As it was asked to share some lines of "Python necessary videos.txt". Here I am sharing few of those lines...

Python Tutorial1 https://youtu.be/*$!@&_

Python Tutorial2 https://youtu.be/@#@$>?

Note that here I've changed the original urls.


Solution

  • cmd = cmd + str(i)
    

    You keep appending new URLs to the command, so in the first iteration you call

    youtube-dl -f 22 url1
    

    and in the next iteration

    youtube-dl -f 22 url1 url2
    

    and so on.

    You should use a different variable name for the base command so that cmd is created new each time, instead of reusing the previous URLs.

    For example:

    base_cmd = 'youtube-dl -f 22 '
    # ...
    cmd = base_cmd + str(i)