from yt_dlp import YoutubeDL
with YoutubeDL() as ydl:
ydl.download('https://youtu.be/0KFSuoHEYm0')
this is the relevant bit of code producing the output.
what I would like to do is grab the 2nd last line from the output below, specifying the video title.
I have tried a few variations of
output = subprocess.getoutput(ydl)
as well as
output = subprocess.Popen( ydl, stdout=subprocess.PIPE ).communicate()[0]
the output I am attempting to capture is the 2nd last line here:
[youtube] 0KFSuoHEYm0: Downloading webpage
[youtube] 0KFSuoHEYm0: Downloading android player API JSON
[info] 0KFSuoHEYm0: Downloading 1 format(s): 22
[download] Destination: TJ Watt gets his 4th sack of the game vs. Browns [0KFSuoHEYm0].mp4
[download] 100% of 13.10MiB in 00:01
There is also documentation on yt-dlp on how to pull title from metadata or include as something in the brackets behind YoutubeDL(), but I can not quite figure it out.
This is part of the first project I am making in python. I am missing an understanding of many concepts any help would be much appreciated.
Credits: answer to question: How to get information from youtube-dl in python ??
Modify your code as follows:
from yt_dlp import YoutubeDL
with YoutubeDL() as ydl:
info_dict = ydl.extract_info('https://youtu.be/0KFSuoHEYm0', download=False)
video_url = info_dict.get("url", None)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
print("Title: " + video_title) # <= Here, you got the video title
This is the output:
#[youtube] 0KFSuoHEYm0: Downloading webpage
#[youtube] 0KFSuoHEYm0: Downloading android player API JSON
#Title: TJ Watt gets his 4th sack of the game vs. Browns
Related
I am trying make a YouTube video downloader.
I want make a progress bar while downloading the YouTube video but I cant get any info (how many MB have been downloaded or how many video MB).
I don't know if this is possible with python or not. Here is my code so far:
import time , os
from pytube import YouTube
from tqdm import tqdm
al = str(input("C4ommand:"))
if al == "4":
af = input("Link:")
you = YouTube(af)
try:
os.mkdir("Download")
except:
pass
time.sleep(2)
time.sleep(1)
res = you.streams.get_highest_resolution()
a = res.download()
with tqdm(total=100) as pbar:
for i in range(10):
time.sleep(0.5)
pbar.update(10)
print(af + "Link downloading....")
b = open("\Download", "w")
b.write(a)
b.close()
print("Downloaded")
To access the progress of the download, you can use the on_progress_callback argument when creating a YouTube instance.
The pytube quickstart says the following:
The on_progress_callback function will run whenever a chunk is downloaded from a video, and is called with three arguments: the stream, the data chunk, and the bytes remaining in the video. This could be used, for example, to display a progress bar.
from pytube import Stream
from pytube import YouTube
from tqdm import tqdm
def progress_callback(stream: Stream, data_chunk: bytes, bytes_remaining: int) -> None:
pbar.update(len(data_chunk))
url = "http://youtube.com/watch?v=2lAe1cqCOXo"
yt = YouTube(url, on_progress_callback=progress_callback)
stream = yt.streams.get_highest_resolution()
print(f"Downloading video to '{stream.default_filename}'")
pbar = tqdm(total=stream.filesize, unit="bytes")
path = stream.download()
pbar.close()
print(f"Saved video to {path}")
Sample output:
Downloading video to 'YouTube Rewind 2019 For the Record YouTubeRewind.mp4'
100%|██████████████████████████████| 87993287/87993287 [00:17<00:00, 4976219.51bytes/s]
Saved video to /tmp/testing/YouTube Rewind 2019 For the Record YouTubeRewind.mp4
Pytube has a built-in progress bar, but it does not use tqdm. Please see https://stackoverflow.com/a/60678355/5666087 for more information.
I'm currently making a program to transfer songs saved on my YT account into a Spotify playlist, and I am using youtube_dl to extract the meta data from the YT videos using the code below:
# use youtube_dl to collect the song name & artist name
video = youtube_dl.YoutubeDL({}).extract_info(
youtube_url, download=False)
song_name = video["track"]
artist = video["artist"]
When I was first made this project in March, the json that resulted from extract_info included the proper artist name, but now the artist, along with many other values (although not necessary for this task) are listed as None. Has anyone run into this issue? I'm considering a work around of not using the artist name to get the uri, but that would make it impossible to distinguish two songs with the same name. If anyone else has noticed this and has found a solution I'd love to hear it!
Setting the user-agent to Facebook's web crawler seems to solve this
youtube_dl.utils.std_headers['User-Agent'] = "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
Here is the video I tried your code with: https://www.youtube.com/watch?v=QBxSQXbj6Go
And the code and output from your snippet below.
Script:
[http_offline#greenhat-32 LEARNING]$ cat tests2.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import youtube_dl
# use youtube_dl to collect the song name & artist name
youtube_url = 'QBxSQXbj6Go'
video = youtube_dl.YoutubeDL({}).extract_info(youtube_url, download=False)
song_name = video["track"]
artist = video["artist"]
print(artist, ' - ', song_name)
[http_offline#greenhat-32 LEARNING]$
Output:
[http_offline#greenhat-32 LEARNING]$ ./tests2.py
[youtube] QBxSQXbj6Go: Downloading webpage
[youtube] QBxSQXbj6Go: Downloading MPD manifest
REO Speedwagon - Keep Pushin'
[http_offline#greenhat-32 LEARNING]$
youtube-dl can't extract Artist, Release Date and other certain fields from any video, it has to be a video with the description format like the video I provided above.
PS. it won't help you, but they released a new youtube-dl version a few days ago, you might want to grab it.
I am trying to make a program that takes YT playlist and play all it's content.
I've installed all components needed for pafy to run with python3. Everything I've tried works as it's expected, except the bellow part of the code.
plurl = "https://www.youtube.com/playlist?list=PL634F2B56B8C346A2"
playlist = pafy.get_playlist(plurl)
url = playlist['items'][21]['pafy'].getbest().url
video = pafy.new(url)
When pafy.new() is called, gives an error because of too long url:
Need 11 character video id or the URL of the video. Got https://r2---sn-bavc5aoxu-nv4l.googlevideo.com/videoplayback?ms=au%2Crdu&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&mv=m&mt=1554899146&requiressl=yes&ip=37.157.173.53&pl=19&id=o-AGQZkyoEvykUGae7O4v_Ycmuj4jJBYdgafcfLBQ5S4Dd&mn=sn-bavc5aoxu-nv4l%2Csn-nv47lnsr&mm=31%2C29&source=youtube&lmt=1387649403290510&ei=POGtXJzdIo_ugAeEiL_wAQ&c=WEB&key=yt6&mime=video%2Fmp4&gir=yes&itag=18&clen=5461830&fvip=2&expire=1554920860&ratebypass=yes&dur=206.100&initcwndbps=1573750&ipbits=0&signature=AAA8B36CD3B402F587F874956595ACB928806C4F.D36C0A79E7F1727DB872425E696DBFC550AA7DF6
Is there a way I can get normal url or video ID ?
The videoid is also available in the url object. You can use
dir(<object>)
to see what properties are available.
id = playlist['items'][2]['pafy'].videoid
video = pafy.new('https://www.youtube.com/watch?v='+id)
use try and catch before using pafy.new , as some of the videos might not be available in the region.
I have the following piece of python code which calls youtube-dl and extracts the links that I need.
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})
with ydl:
result = ydl.extract_info(
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
if video:
return video
return None
But I want to use the custom User-Agent in this program. I know I can specify the custom User-Agent while using the youtube-dl in the command line.
Is there any way I can specify the custom user-agent in the program embedding youtube-dl.
Thanks
I used Github's code search to find user-agent in the YTDL codebase, ended up finding this piece of code that sets the user agent based on the command line.
So, all in all, just
import youtube_dl.utils
youtube_dl.utils.std_headers['User-Agent'] = 'my-user-agent'
to override it.
I'm a newbie to python and still on the process of learning on the go ...
I have a webserver which has list of images to load on a Device Under Test (DUT) ...
Requirement is:
if the image is already present on the server, proceed with loading the image onto the DUT.
if the image is not present on the server , then proceed with the download of the image and then upgrade the DUT.
I have written the following code but I'm quite not happy with the way I have written this, because I have a feeling that it could have been done better using some other method/s
Please suggest the areas where i could have done better and the techniques to do so..
Appreciate your time in reading this email and for your valuable suggestions.
import urllib2
url = 'http://localhost/test'
filename = 'Image60.txt' # image to Verify
def Image_Upgrade():
print 'proceeding with Image upgrade !!!'
def Image_Download():
print 'Proceeding with Image Download !!!'
resp = urllib2.urlopen(url)
flag = False
list_of_files = []
for contents in resp.readlines():
if 'Image' in contents:
c=(((contents.split('href='))[-1]).split('>')[0]).strip('"') # The content output would have html tags. so removing the tags to pick only image name
if c != filename:
list_of_files.append(c)
else:
Image_Upgrade()
flag = True
if flag==False:
Image_Download()
Thanks,
Vijay Swaminathan