ytdl-org / youtube-dl

Command-line program to download videos from YouTube.com and other video sites
http://ytdl-org.github.io/youtube-dl/
The Unlicense
132.41k stars 10.04k forks source link

How do I get the name of final file in Python? #16658

Closed NightMachinery closed 6 years ago

NightMachinery commented 6 years ago

I am using this currently:

        def my_hook(d):
                        if d['status'] == 'finished':
                            d1 = d['filename']
                            ...
                    ydl_opts = {
                        'progress_hooks': [my_hook],
                        'outtmpl': 'dls/%(playlist_title)s_%(title)s_%(format)s_%(autonumber)s.%(ext)s'
                    }
                    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                        d2 = ydl.extract_info(url)

But d2 doesn't have any filename attributes and my_hook is called two times when audio and video files are downloaded separately and then merged, and it is actually not called for the final, merged file.

NightMachinery commented 6 years ago

I tried this as well:

                   file_name = 'dls/' + str(uuid.uuid4())
                    ydl_opts = {
                        'outtmpl': file_name
                    }
                    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                        d2 = ydl.extract_info(url)
                        file_name_with_ext = file_name + "." + d2['ext']
                        ...

It works sometimes, but when the file is merged into an mkv, d2['ext'] wrongly returns 'mp4'.

dstftw commented 6 years ago

https://github.com/rg3/youtube-dl/issues/10987#issuecomment-284041577