Python Forum
Download mp4 files from an url
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Download mp4 files from an url
#1
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)
Yoriz write Mar-07-2022, 06:33 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

Thumbnail(s)
   
Reply
#2
(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).
Reply
#3
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}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Opinion: how should my scripts cache web download files? stevendaprano 0 702 Dec-17-2022, 12:19 AM
Last Post: stevendaprano
  How to download a list of files from FTP? schnarkle 0 974 Jun-21-2022, 10:35 PM
Last Post: schnarkle
  download with internet download manager coral_raha 0 2,880 Jul-18-2021, 03:11 PM
Last Post: coral_raha
  How can I download Python files from GitHub? bitcoin10mil 2 2,767 Aug-26-2020, 09:03 PM
Last Post: Axel_Erfurt
  Download multiple large json files at once halcynthis 0 2,751 Feb-14-2019, 08:41 AM
Last Post: halcynthis
  API to download files ahantge 1 2,642 Apr-13-2018, 08:55 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020