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

Import youtube_dl.YoutubeDL External Python Program #4662

Closed
HPP8140 opened this issue Jan 8, 2015 · 9 comments
Closed

Import youtube_dl.YoutubeDL External Python Program #4662

HPP8140 opened this issue Jan 8, 2015 · 9 comments

Comments

@HPP8140
Copy link

@HPP8140 HPP8140 commented Jan 8, 2015

Hi,
So I am integrating youtube-dl with the Youtube plugin for the PyLoad download manager, because youtube-dl supports DASH video.

I have the code working below. Questions:

  1. "137+bestaudio" does not work here for the 'format' like the command line. Is there a way to get this to return the video and audio like the command line?

If not, I can just check for DASH and call the code below a 2nd time with "bestaudio" for the 'format'

  1. Running the following from the command line automatically calls the post-processor to merge the video and audio after downloading.

"youtube-dl -f 137+bestaudio/best --youtube-include-dash-manifest --prefer-ffmpeg https://www.youtube.com/watch?v=B99SxGxHs6U"

Can I still leverage the youtube-dl post-processor to merge the video and audio using the code below? I realize using PyLoad external download manager may have created problems with timing for merging. Youtube-dl downloads the video 1st and then calls the postprocessor after the audio is downloaded and merges.


    import youtube_dl

    ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s','format': '137/best'})

    with ydl:
        result = ydl.extract_info(
            pyfile.url,
            download=False # We just want to extract the info
        )

    if 'entries' in result:
        # Can be a playlist or a list of videos
        video = result['entries'][0]
    else:
        # Just a video
        video = result

    video_url = video['url']
    video_format = video['format']

    self.download(video_url)
@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented Jan 8, 2015

This is not documented anywhere so it may change in the future:

  • If the dash formats are found, the info is stored in the requested_formats field:
import youtube_dl

ydl_opts = {
        'format': 'bestvideo+bestaudio/best'
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    test_url = 'http://www.youtube.com/watch?v=BaW_jenozKc'
    info = ydl.extract_info(test_url, download=False)

if info.get('requested_formats') is not None:
    # we got DASH formats
    video_format, audio_format = info['requested_formats']
    video_url = video_format['url']
    audio_url = audio_format['url']
else:
    video_url = info['url']
  • If you run ydl.extract_info(test_url, download=True) instead, the dash formats will be merged (but they will be downloaded by youtube-dl itself)
@jaimeMF jaimeMF closed this Jan 8, 2015
@HPP8140
Copy link
Author

@HPP8140 HPP8140 commented Jan 8, 2015

Thank You for the helpful code, however; this version appears to ignore the "download=False" and continues to download.

@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented Jan 8, 2015

It works fine here (and should work with quite old versions), could you run this script and post the output?:

from youtube_dl.version import __version__
print('version: %s' % __version__)

import youtube_dl

ydl_opts = {
        'format': 'bestvideo+bestaudio/best',
        'verbose': True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    test_url = 'http://www.youtube.com/watch?v=BaW_jenozKc'
    ydl.print_debug_header()
    info = ydl.extract_info(test_url, download=False)

if info.get('requested_formats') is not None:
    # we got DASH formats
    video_format, audio_format = info['requested_formats']
    video_url = video_format['url']
    audio_url = audio_format['url']
else:
    video_url = info['url']

Posting the script you are using would help.

@HPP8140
Copy link
Author

@HPP8140 HPP8140 commented Jan 9, 2015

Sorry, my mistake...forgot to disable a bash script that was calling youtube-dl. The code works perfectly!!!

Now to figure out how to combine the audio and video. I ran youtube-dl with the --verbose option, which gave me the command to use.
(avconv -y -i 'filename.mp4' -i 'filename.m4a' -c copy -map 0✌️0 -map 1🅰️0 -shortest 'filename.mp4')

This allows me to combine the wonderful Web and Android GUI of pyLoad with the powerful extractor of youtube-dl.

@HPP8140
Copy link
Author

@HPP8140 HPP8140 commented Jan 9, 2015

Cool, I can make use of all these available options in ydl_opts.

Available options:

username:          Username for authentication purposes.
password:          Password for authentication purposes.
videopassword:     Password for acces a video.
usenetrc:          Use netrc for authentication instead.
verbose:           Print additional info to stdout.
quiet:             Do not print messages to stdout.
no_warnings:       Do not print out anything for warnings.
forceurl:          Force printing final URL.
forcetitle:        Force printing title.
forceid:           Force printing ID.
forcethumbnail:    Force printing thumbnail URL.
forcedescription:  Force printing description.
forcefilename:     Force printing final filename.
forceduration:     Force printing duration.
forcejson:         Force printing info_dict as JSON.
dump_single_json:  Force printing the info_dict of the whole playlist
                   (or video) as a single JSON line.
simulate:          Do not download the video files.
format:            Video format code. See options.py for more information.
format_limit:      Highest quality format to try.
outtmpl:           Template for output names.
restrictfilenames: Do not allow "&" and spaces in file names
ignoreerrors:      Do not stop on download errors.
nooverwrites:      Prevent overwriting files.
playliststart:     Playlist item to start at.
playlistend:       Playlist item to end at.
playlistreverse:   Download playlist items in reverse order.
matchtitle:        Download only matching titles.
rejecttitle:       Reject downloads for matching titles.
logger:            Log messages to a logging.Logger instance.
logtostderr:       Log messages to stderr instead of stdout.
writedescription:  Write the video description to a .description file
writeinfojson:     Write the video description to a .info.json file
writeannotations:  Write the video annotations to a .annotations.xml file
writethumbnail:    Write the thumbnail image to a file
writesubtitles:    Write the video subtitles to a file
writeautomaticsub: Write the automatic subtitles to a file
allsubtitles:      Downloads all the subtitles of the video
                   (requires writesubtitles or writeautomaticsub)
listsubtitles:     Lists all available subtitles for the video
subtitlesformat:   Subtitle format [srt/sbv/vtt] (default=srt)
subtitleslangs:    List of languages of the subtitles to download
keepvideo:         Keep the video file after post-processing
daterange:         A DateRange object, download only if the upload_date is in the range.
skip_download:     Skip the actual download of the video file
cachedir:          Location of the cache files in the filesystem.
                   False to disable filesystem cache.
noplaylist:        Download single video instead of a playlist if in doubt.
age_limit:         An integer representing the user's age in years.
                   Unsuitable videos for the given age are skipped.
min_views:         An integer representing the minimum view count the video
                   must have in order to not be skipped.
                   Videos without view count information are always
                   downloaded. None for no limit.
max_views:         An integer representing the maximum view count.
                   Videos that are more popular than that are not
                   downloaded.
                   Videos without view count information are always
                   downloaded. None for no limit.
download_archive:  File name of a file where all downloads are recorded.
                   Videos already present in the file are not downloaded
                   again.
cookiefile:        File name where cookies should be read from and dumped to.
nocheckcertificate:Do not verify SSL certificates
prefer_insecure:   Use HTTP instead of HTTPS to retrieve information.
                   At the moment, this is only supported by YouTube.
proxy:             URL of the proxy server to use
socket_timeout:    Time to wait for unresponsive hosts, in seconds
bidi_workaround:   Work around buggy terminals without bidirectional text
                   support, using fridibi
debug_printtraffic:Print out sent and received HTTP traffic
include_ads:       Download ads as well
default_search:    Prepend this string if an input url is not valid.
                   'auto' for elaborate guessing
encoding:          Use this encoding instead of the system-specified.
extract_flat:      Do not resolve URLs, return the immediate result.
                   Pass in 'in_playlist' to only show this behavior for
                   playlist items.
postprocessors:    A list of dictionaries, each with an entry
                   * key:  The name of the postprocessor. See
                           youtube_dl/postprocessor/__init__.py for a list.
                   as well as any further keyword arguments for the
                   postprocessor.
progress_hooks:    A list of functions that get called on download
                   progress, with a dictionary with the entries
                   * filename: The final filename
                   * status: One of "downloading" and "finished"

                   The dict may also have some of the following entries:

                   * downloaded_bytes: Bytes on disk
                   * total_bytes: Size of the whole file, None if unknown
                   * tmpfilename: The filename we're currently writing to
                   * eta: The estimated time in seconds, None if unknown
                   * speed: The download speed in bytes/second, None if
                            unknown

                   Progress hooks are guaranteed to be called at least once
                   (with status "finished") if the download is successful.


The following parameters are not used by YoutubeDL itself, they are used by
the FileDownloader:
nopart, updatetime, buffersize, ratelimit, min_filesize, max_filesize, test,
noresizebuffer, retries, continuedl, noprogress, consoletitle

The following options are used by the post processors:
prefer_ffmpeg:     If True, use ffmpeg instead of avconv if both are available,
                   otherwise prefer avconv.
exec_cmd:          Arbitrary command to run after downloading
"""
@HPP8140
Copy link
Author

@HPP8140 HPP8140 commented Jan 9, 2015

One last question...

Looks like there is a progress hook. Can I get that percentage using this code?

Thank You

@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented Jan 9, 2015

Yes:

def hook(ph):
    percent = float(ph['downloaded_bytes'])/float(ph['total_bytes']) * 100.0
@HPP8140
Copy link
Author

@HPP8140 HPP8140 commented Jan 10, 2015

Thanks again.

@adielp89
Copy link

@adielp89 adielp89 commented Jan 25, 2017

Hello @HPP8140 @jaimeMF ,
I have mounted pyload in my work center, I would like help me how to integrate youtube-dl to pyload to download from video sites with this tool using pyload

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.