Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tech support] How to get filename after a download has completed (Python) #16401

Closed
devxpy opened this issue May 8, 2018 · 5 comments
Closed

[Tech support] How to get filename after a download has completed (Python) #16401

devxpy opened this issue May 8, 2018 · 5 comments

Comments

@devxpy
Copy link

@devxpy devxpy commented May 8, 2018

  • I've verified and I assure that I'm running youtube-dl 2018.05.01

Before submitting an issue make sure you have:

  • At least skimmed through the README, most notably the FAQ and BUGS sections
  • Searched the bugtracker for similar issues including closed ones
  • Checked that provided video/audio/playlist URLs (if any) are alive and playable in a browser

What is the purpose of your issue?

  • Bug report (encountered problems with youtube-dl)
  • Site support request (request for adding support for a new site)
  • Feature request (request for a new functionality)
  • Question
  • Other

Hello,
I want to download audio files using Python (using --extract-audio) and then get the final filename after youtube-dl / ffmpeg are done with it.

Example
subprocess.run([
                'youtube-dl',
                '--extract-audio', '--restrict-filenames',
                '-o', DL_DIR.joinpath(f'{song.spotify_id}.%(ext)s'),
                song.youtube_id
            ])

output-

[youtube] mMoVvCEYMKM: Downloading webpage
[youtube] mMoVvCEYMKM: Downloading video info webpage
[youtube] mMoVvCEYMKM: Extracting video information
[download] /home/dev/Projekt/encadio/downloads/English/4gLuKrWf0Ub3nwrRB0yJk7.m4a has already been downloaded
[download] 100% of 3.27MiB
[ffmpeg] Correcting container in "/home/dev/Projekt/encadio/downloads/English/4gLuKrWf0Ub3nwrRB0yJk7.m4a"
[ffmpeg] Post-process file /home/dev/Projekt/encadio/downloads/English/4gLuKrWf0Ub3nwrRB0yJk7.m4a exists, skipping

Now how do I know what the final filename (or just the file extension) was, after the download finished?

I could also do this, as suggested by readme

with youtube_dl.YoutubeDL(ydl_opts) as ydl:                
    ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])

But I couldn't find the final filename in ydls attributes.

I could have my application work by just searching using song.spotify_id, but that seems a little janky?
@devxpy
Copy link
Author

@devxpy devxpy commented May 10, 2018

Sorry, my bad. Didn't see the full example.
It's available like this

    def my_hook(d):
        if d['status'] == 'finished':
            print(d['filename'])

    ydl_opts = {
        'format': 'bestaudio',
        'progress_hooks': [my_hook],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])
@devxpy devxpy closed this May 10, 2018
@Moriand
Copy link

@Moriand Moriand commented May 24, 2018

please a complete example, I need to understand it completely

@devxpy
Copy link
Author

@devxpy devxpy commented May 24, 2018

Just import youtube_dl I guess..

@Moriand
Copy link

@Moriand Moriand commented May 24, 2018

Well, well, you go there, rather I wanted to know what more options it offers, for example:
I want to download the subtitle
as after download, I convert to mp3 or other audio formats

@devxpy
Copy link
Author

@devxpy devxpy commented May 24, 2018

Never tried doing subtitles, will try..

If you convert to other format, d['filename'] returns the filename for converted format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.