-2

I want to download a YouTube video using Python. I use this code to scrape images and it worked fine. But when i changed this image link to youtube video then it saves the file, but makes an error corrupted mp4 file. Any idea what i am doing wrong?

Here's my code :

import requests
import shutil
import time
import urllib

video_url = r"https://i.ytimg.com/vi/hoNb6HuNmU0/hq720.jpg?sqp=-oaymwEZCNAFEJQDSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLBTdEdTL4oiRB_L8AJITd9xzfllMA"
resp = requests.get(video_url, stream=True)
local_file = open(r'C:/users/intel/desktop/local_image.mp4', 'wb')
resp.raw.decode_content = True
shutil.copyfileobj(resp.raw, local_file)
del resp

Any of your help would be appreciated...

Abhay Salvi
  • 890
  • 3
  • 15
  • 39

1 Answers1

0

Your approach won't work.

Check https://github.com/ytdl-org/youtube-dl for the proper way to download YouTube videos.

lenik
  • 23,228
  • 4
  • 34
  • 43