How to get info about video, without downloading it, using youtube-dl

Question:

I made my own script using python and youtube-dl library to easily download songs from online radio. It works by copying a playlist, saving it to a .txt file, and then running a script that goes through the playlist and discards the time for each line, and then searches youtube with the song name and downloads the first one it finds.
Since a lot of songs are repeated, the program downloads the song again and processes it (converts it to mp3 and isolates only the audio) and overwrites the old one. I’m interested in how to retrieve just the video name without downloading, and check if that song has already been downloaded to avoid wasting time and resources unnecessarily. I know that the "" team has songs ready, and I’m not sure if it can be used differently. Below is a .py script and an example of a song list (the list usually has 500 lines, so there is a lot of repetition).

<main.py>

import os
radio = "BBCRadio1"
lista = []
with open ("./list.txt" ,"r", encoding="utf8") as f:
    lines = f.readlines()
    for line in lines:
        lista.append(line.split("t")[1].strip())
lista = list(dict.fromkeys(lista))
for song in lista:
    print("------------------------------------------Downloading " + str(lista.index(song) + 1) + ". song of " + str(len(lista)) + " songs------------------------------------------")
    os.system("youtube-dl  -o "D:ProjectsYT-DL/songs/" + radio + "/%(title)s.%(ext)s"  --extract-audio --audio-format mp3 "ytsearch:" + song + """)
<BBCRadio1.txt>

14:32   Harry Styles - Watermelon Sugar
14:27   Drake - Passionfruit
14:24   Phoebe Bridgers - I See You
14:20   Avicii - Lonely Together (feat. Rita Ora)
14:17   Jawsh 685 & Jason Derulo - Savage Love (Laxed - Siren Beat)
14:13   Clipz, Ms. Dynamite, Ms Banks & JayKae - Again
14:09   Katy Perry - Chained To The Rhythm (feat. Skip Marley)
14:06   Dave - Funky Friday (Glastonbury 2019)
14:02   Dave - Location (Glastonbury 2019) (feat. Burna Boy)
13:58   Topic - Breaking Me (feat. A7S)
13:54   Lily Allen - The Fear
13:49   The Weeknd - I Feel It Coming (feat. Daft Punk)
13:46   Sports Team - Going Soft
13:42   Megan Thee Stallion & Beyoncé - Savage (Remix)
13:38   Jonas Brothers - Sucker
Asked By: Josip Maričević

||

Answers:

I’m not sure why you’re using youtube-dl to get video metadate. You can use os.path.isfile(fname) to see if the file exists in the directory before attempting download it. That way it’ll avoid duplicates
https://stackoverflow.com/a/82852/8416255

Or you could use the youtube-dl python library and set download=False
https://stackoverflow.com/a/61974543/8416255

Answered By: rsn

You should try the approach the ytdl already has it for skipping already downloaded files from a playlist. In short use, --download-archive feature, source. Example below:

youtube-dl --download-archive archive.txt "https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re"

Here archive.txt file will hold all the videos already downloaded from a playlist.

Answered By: Mrinal Roy

Did you meant this?

youtube-dl --write-annotations --write-info-json --skip-download www.youtube.com/yourvideo

--write-annotations get annotations
--write-info-json save the metadata in a json file
--skip-download to skip the download of the video

if you only want the title, you can use the option
-e or --get-title that "Simulate, quiet but print title"

if you want the json information without downloading it you can use
-j or --dump-json to "Simulate, quiet but print JSON information. See the "OUTPUT TEMPLATE" for a description of available keys".
-J or --dump-single-json to "Simulate, quiet but print JSON information for each command-line argument. If the URL refers to a playlist, dump the whole playlist information in a single line."

Answered By: alisson_Soares

You can also use: yt_dlp it’s a fork of youtube_dl, but downloads are very fast. Also in example bellow you will see the: hook function, so after completed download, you can process it as you want.

Example bellow:

import yt_dlp # pip install yt_dlp

def hook(d):
    if d['status'] == 'finished':
        filename = d['filename']
        print(filename) # Here you will see the PATH where was saved.

def client(video_url, download=False):
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
         ydl.extract_info(video_url, download=download)

ydl_opts = { 
        'format': 'bestaudio/best',
        'outtmpl': '%(title)s.%(ext)s', # You can change the PATH as you want
        'download_archive': 'downloaded.txt',
        'noplaylist': True,   
        'quiet': True,
        'no_warnings': True,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'progress_hooks': [hook]
}

now you can use your list:

for song in lista:
    song_details = client(f'ytsearch:{song}', download=False) # get the json details about the song  
    download     = client(f'ytsearch:{song}', download=True)  # download the song

    song_details['title']
    song_details['format_note']
    song_details['audio_ext']
    song_details['filesize']
Answered By: Cornea Valentin
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.