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

Question : Extract the video title without additional requests? #4988

Closed
Elite opened this issue Feb 18, 2015 · 6 comments
Closed

Question : Extract the video title without additional requests? #4988

Elite opened this issue Feb 18, 2015 · 6 comments

Comments

@Elite
Copy link

@Elite Elite commented Feb 18, 2015

I would like to know how to extract the video title without additional requests using the official example as below:

import youtube_dl


class MyLogger(object):
    def debug(self, msg):
        pass

    def warning(self, msg):
        pass

    def error(self, msg):
        print(msg)


def my_hook(d):
    if d['status'] == 'finished':
        print('Done downloading, now converting ...')


ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
    'logger': MyLogger(),
    'progress_hooks': [my_hook],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
@phihag
Copy link
Contributor

@phihag phihag commented Feb 18, 2015

You don't need any of the advanced options / hooks for that. Simply call extract_info, like this:

import youtube_dl


ydl_opts = {
    'quiet': True,
    'skip_download': True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    info = ydl.extract_info('http://www.youtube.com/watch?v=BaW_jenozKc')

print('Title of the extracted video/playlist: %s' % info['title'])
@phihag phihag closed this Feb 18, 2015
@Elite
Copy link
Author

@Elite Elite commented Feb 19, 2015

Thanks for the information but my question is how to Download the video and extract the information in a single step without multiple page-fetches. The below works for me but is slow and also ignores the 'continuedl': True, directive.

    def download(self):
        #self.statusSignal.emit('Starting...')
        ydl_options = {
            'outtmpl': '{0}%(title)s-%(id)s.%(ext)s'.format(self.directory),
            'format': 'bestaudio',
            'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
            }],
            'continuedl': True,
            'quiet' : True,
            'restrictfilenames':True, 
        }
        with youtube_dl.YoutubeDL(ydl_options) as ydl:
            info = ydl.extract_info(self.url)
            self.statusSignal.emit(info['title'])
            ydl.add_default_info_extractors()
            ydl.add_progress_hook(self.hook)
            try:
                ydl.download([self.url])
            except (youtube_dl.utils.DownloadError,youtube_dl.utils.ContentTooShortError,youtube_dl.utils.ExtractorError) as e:
                self.error_occured = True
                self.statusSignal.emit(str(e))
@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented Feb 19, 2015

extract_info already downloads the video by default, try with this:

import youtube_dl
url = 'test:youtube'
ydl_options = {
            'outtmpl': '%(title)s-%(id)s.%(ext)s',
            'format': 'bestaudio',
            'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
            }],
            'continuedl': True,
            'quiet' : True,
            'restrictfilenames':True, 
        }
def hook(s):
    print(s)
with youtube_dl.YoutubeDL(ydl_options) as ydl:
    try:
        ydl.add_default_info_extractors()
        ydl.add_progress_hook(hook)
        info = ydl.extract_info(url,
            download=True # it's the default value, but to make it clearer
        )
        print(info['title'])
    except (youtube_dl.utils.DownloadError,youtube_dl.utils.ContentTooShortError,youtube_dl.utils.ExtractorError) as e:
        self.error_occured = True
        self.statusSignal.emit(str(e))

The reason why it downloads the video again is that the postprocessor deletes the original file, so we can't detect if it has been downloaded, you can use the keepvideo option for that.

@Elite
Copy link
Author

@Elite Elite commented Feb 21, 2015

Thanks @jaimeMF, this works but does not prints the title nor emits the signal!!!

    def download(self):
        #self.statusSignal.emit('Starting...')
        ydl_options = {
            'outtmpl': '{0}%(title)s-%(id)s.%(ext)s'.format(self.directory),
            'format': 'bestaudio',
            'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
            }],
            'continuedl': True,
            'quiet' : True,
            'restrictfilenames':True, 
        }
        with youtube_dl.YoutubeDL(ydl_options) as ydl:
            try:
                ydl.add_default_info_extractors()
                ydl.add_progress_hook(self.hook)
                info = ydl.extract_info(self.url, download=True )
                self.statusSignal.emit(info['title'])
                print(info['title'])
            except (youtube_dl.utils.DownloadError,youtube_dl.utils.ContentTooShortError,youtube_dl.utils.ExtractorError) as e:
                self.error_occured = True
                self.statusSignal.emit(str(e))

@Elite
Copy link
Author

@Elite Elite commented Feb 23, 2015

@jaimeMF @phihag any pointers guys?

@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented Feb 23, 2015

I have adapted the script to remove the self references and it works fine:

import youtube_dl

def download(url):
        #self.statusSignal.emit('Starting...')
        ydl_options = {
            'outtmpl': '%(title)s-%(id)s.%(ext)s',
            'format': 'bestaudio',
            'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
            }],
            'continuedl': True,
            'quiet' : True,
            'restrictfilenames':True, 
        }
        def hook(s):
            print(s)
        with youtube_dl.YoutubeDL(ydl_options) as ydl:
            try:
                ydl.add_default_info_extractors()
                ydl.add_progress_hook(hook)
                info = ydl.extract_info(url, download=True )
                print(info['title'])
            except (youtube_dl.utils.DownloadError,youtube_dl.utils.ContentTooShortError,youtube_dl.utils.ExtractorError) as e:
                print(e)

download('test:youtube')

Could you try with that script and post the output?

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
3 participants
You can’t perform that action at this time.