Python Forum
Download mp4 files from an url - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Download mp4 files from an url (/thread-36580.html)



Download mp4 files from an url - q988988 - Mar-07-2022

I am trying to get some codes to be able to: enter the time frame (e.g. 2021-11-25 11:00 to 2021-11-29 02:19) -> find the links corresponding to those time stamps within the range -> download the .mp4 file under each link.

I have come up with some codes trying to download the mp4 files first, then I will fugure out how to download the files with specific time stamps, but I seemed to get stuck in the first step of just downloading mp4 files.

Here are the codes:

i
mport requests 
from bs4 import BeautifulSoup 
  
Video_url = "https://file-examples.com/index.php/sample-video-files/sample-mp4-files/"
  
def get_video_links(): 
      
    # create response object 
    r = requests.get(Video_url) 
      
    # create beautiful-soup object 
    soup = BeautifulSoup(r.content,'html5lib') 
      
    # find all links on web-page 
    links = soup.findAll('a') 
  
    # filter the link ending with .mp4 
    video_links = [Video_url + link['href'] for link in links if link['href'].endswith('mp4')] 
  
    return video_links 

def download_video_series(video_links): 
  
    for link in video_links: 
  
        '''iterate through all links in video_links 
        and download them one by one'''
          
        # obtain filename by splitting url and getting 
        # last string 
        file_name = link.split('/')[-1] 
  
        print( "Downloading file:%s"%file_name) 
          
        # create response object 
        r = requests.get(link, stream = True) 
          
        # download started 
        with open(file_name, 'wb') as f: 
            for chunk in r.iter_content(chunk_size = 1024*1024): 
                if chunk: 
                    f.write(chunk) 
          
        print( "%s downloaded!\n"%file_name )
  
    print ("All videos downloaded!")
    return
if __name__ == "__main__": 
  
    # getting all video links 
    video_links = get_video_links() 
  
    # download all videos 
    download_video_series(video_links)



RE: Download mp4 files from an url - ibreeden - Mar-07-2022

(Mar-07-2022, 05:14 AM)q988988 Wrote: I seemed to get stuck in the first step of just downloading mp4 files
You forgot to tell us what went wrong. Did you get an error message? Then show the (complete) error message (in the right BBcode tags).


RE: Download mp4 files from an url - snippsat - Mar-07-2022

You most look at what you first function return.
>>> get_video_links()[0]
'https://file-examples.com/index.php/sample-video-files/sample-mp4-files/https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4'
Dos that look like working download link?
The test is simple paste into browser as see what happens.

Here is fix for your first function then can try to figure how to download.
import requests
from bs4 import BeautifulSoup

def video_links(video_url):
    response = requests.get(video_url)
    soup = BeautifulSoup(response.content, 'html5lib')
    for url_link in soup.find_all(class_="text-right file-link"):
        yield url_link.a.get('href', 'Something went wrong')

def download_video_series(video_links):
    pass

if __name__ == '__main__':
    video_url = "https://file-examples.com/index.php/sample-video-files/sample-mp4-files/"
    for link in video_links(video_url):
        print(link)
Output:
https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4 https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_640_3MG.mp4 https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1280_10MG.mp4 https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1920_18MG.mp4
Also don't use the old sting formatting(%s) anymore.
print("Downloading file:%s" % file_name) 
# f-string
print(f"Downloading file: {file_name}")