problem with downloading videos that had same title in youtube playlist with pytube

Question:

In youtube playlist, if videos had same title, existing/downloaded video is replaced by the new video.

Example: a playlist consist 2 youtube videos : A & A

First A isn’t showing up in files because second A replaced first video.

I wanted solve this problem by renaming the file with a number before video title
Like this [A -> 1A] [A -> 2A]
Please someone tell me how do it.

Asked By: karthik oggu

||

Answers:

I am assuming you are iterating over a Playlist object like so:

p = Playlist('https://www.youtube.com/watch?v=41qgdwd3zAg&list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n')
for video in p.videos:
     video.streams.first().download()

Example from the docs.

Then the download method takes an argument for filename_prefix where you can name the file yourself:

prefixes = {}

for video in p.videos:
    if video.title() in prefixes:
        video.streams.first().download(filename_prefix=str(prefixes[title]))
        prefixes[video.title()] += 1
    else:
        video.streams.first().download(filename_prefix='1')
        prefixes[video.title()] = 2

You could do something like this, however all of your videos would be prefixed with a number this way. You could add a suffix instead by using filename=video().title() + str(suffix), because there is no suffix parameter on download.

Answered By: calvinsomething
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.