0

I've downloaded some files using requests

url = 'https://www.youtube.com/watch?v=gp5tziO5lXg&feature=youtu.be'
video_name = url.split('/')[-1]

print("Downloading file:%s" % video_name)

# download the url contents in binary format
r = requests.get(url)

# open method to open a file on your system and write the contents
with open('saved.mp4', 'wb') as f:
    f.write(r.content)

and using urllib.requests

url = 'https://www.youtube.com/watch?v=gp5tziO5lXg&feature=youtu.be'
video_name = url.split('/')[-1]

print("Downloading file:%s" % video_name)

# Copy a network object to a local file
urllib.request.urlretrieve(url, "saved2.mp4")

When I then try to open the .mp4 file I get the following error

Cannot play

This file cannot be played. This can happen because the file type is not supported, the file extension is incorrect or the file is corrupted.

0xc00d36c4

Cannot open .mp4 file

If I test it with pytube it works fine.

What's wrong with the other methods?

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
  • 4
    You are dowloading a web page, not a video... Thanks for the musical discovery anyway ;) – Thierry Lathuille Jun 15 '20 at 12:35
  • 1
    You can have a look at how [youtube_dl](https://github.com/ytdl-org/youtube-dl/) does it, it's written in Python - or directly use it. – Thierry Lathuille Jun 15 '20 at 12:43
  • Interesting library @ThierryLathuille, having only youtube in their name makes it look less valuable than what it really is considering they work also for Google Drive, Dropbox, etc. Would be great if it also worked with One Drive too! – Tiago Martins Peres Jun 15 '20 at 12:52
  • 1
    The problem it solves is to get to the video data starting from the Youtube page. Every site that provides videos will do it in a different way, so you will have to use specific code. – Thierry Lathuille Jun 15 '20 at 12:54
  • They mention to support more websites other than YouTube - http://ytdl-org.github.io/youtube-dl/supportedsites.html – Tiago Martins Peres Jun 15 '20 at 13:03

2 Answers2

1

To answer your question, with the other methods it is not downloading the video but the page. What you may be obtaining is an html file with an mp4 file extension.

Therefore, it gives that error when trying to open the file.

If pytube works for what you need, I would suggest using that one.

If you want to download videos from other platforms, you might consider youtube-dl.

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
-2

Hello you can import IPython.display for audio diplay

import IPython.display as ipd
ipd.Audio(video_name)

regards I hope I can have solved your problem