Get youtube video url form webpage source

25,203

Solution 1

I have written a simple python code that can fetch youtube href from html code. hope it can help you:

#!/usr/bin/env python

import re

pattern = '.*<(iframe|param).*(src|value)="(?P<link>http://www.youtube.com/(embed|v)/[a-zA-Z0-9/\.\?&;=\+_-]+);?.*".*>.*</(iframe|param)>.*'

action = re.compile(pattern)
result = action.findall('<div><iframe.....></iframe><param......></param></div>')

print result 

https://gist.github.com/Mortezaipo/5707738

Solution 2

You should use the player API https://developers.google.com/youtube/js_api_reference#Retrieving_video_information

player.getVideoUrl():String

Returns the YouTube.com URL for the currently loaded/playing video.

Solution 3

Sorry I looked around and I couldn't find a way to directly download the source file from the youtube link. There are some guides out there but they all are around 2009; and since youtube changes their code a lot currently none of them work.

Although there are many websites, addons, apps that allow you to download videos; but I do not think they work from a code from the link. Since to watch a video you have to temporarily download it I think they all use a method of opening the page, playing(downloading) the video and actually saving it. I would list those websites and addons but since those sources are not that hard to find; I assume you are trying to build your own downloader and not use one that is already out there.

Also on another note if you could somehow get a link to a videos source file. I am sure youtube has permissions blocked access on all folders and files on their backend for security measures; only allowing those files to be forwarded and displayed on the actual player page. And if you think embedded videos would somehow work they still just make a request to youtube which youtube then gives back video-feed-data so there still is no direct connection to the file.

Share:
25,203
user1767260
Author by

user1767260

Updated on March 31, 2020

Comments