Downloading Kaltura videos

04 Jan 2025

Working method

  1. Open the page with the video in the browser.
  2. Open the developer tools and go to the Network tab.
  3. Look for a request that ends with .m3u8 and copy the URL.
  4. Make sure you have ffmpeg installed on your system. You can download it from the official website or install it using a package manager like Homebrew on macOS:
brew install ffmpeg
  1. Then run this Python script to download the video:
import os
import subprocess

def download_hls_stream(m3u8_url, output_path):
    try:
        subprocess.run(
            ["ffmpeg", "-i", m3u8_url, "-c", "copy", output_path],
            check=True
        )
        print(f"Download complete. File saved to {output_path}.")
    except subprocess.CalledProcessError as e:
        print(f"Error downloading video: {e}")

if __name__ == "__main__":
    # Replace with your m3u8 URL
    m3u8_url = "https://www.kaltura.com/p/5837132/sp/583713200/playManifest/entryId/1_mavz1jq4/protocol/https/format/applehttp/flavorIds/1_xy0tc2ny,1_daq3ijhv/ks/djJ8NTgzNzEzMnxn3KBLl7L5xN6J8yioU1sSCav-KVy_SHf1O5FyOF9vwQ65xGUz9LFggB-CJHuzxoJMk_slAoyv_yGT2vO99buEmVkC-PfZBPZHX2t2lbBajYhMcFP5JbK-ajh3SbyKmEJu3bBM_UTs-BFw83iDxoBjDPd1A90IHPGv6XmLhRcRe9lLtOJ2O__dp9FZ5sHxHgsIajggsb4fb8wSloWWNy6IYRUDAKGhdFZ1o37DhdHsUb3up-po2zNd1hOPzkj61Z_oJ5YQAmzdlUf6z_gW9no-J3_WSN5nQ7lK6HlKbktyz-Kd19oLgtFfex_cFyOzSjxempEdsePVYaYW_MY0kV-vonWQfbq_GSjhRJzAUPVcTdntZGvQobPEIu1XuJ2Zra-tRRZrX9ZlNFAg1qGFiWZYU=/a.m3u8?uiConfId=545512&playSessionId=383562-c5f2-3fa7-a553-93ab84f6b58b:ab2636e4-057e-90fa-b54b-f4f8841f7c7e&referrer=&clientTag=html5:v7.149"

    # Define the output file path
    output_path = os.path.expanduser("/path/to/video.mp4")

    # Download the video
    download_hls_stream(m3u8_url, output_path)

(link above has been scrambled, but structure is the same)

To automate further / To test

To automate fetching the .m3u8 link, you can use Python with selenium to scrape the video page and locate the link in the network requests programmatically. Here’s how to set it up:

  1. Install Required Libraries
pip install selenium
pip install browsermob-proxy
  1. Script to Fetch .m3u8 Link and Download Video

This script uses selenium with browsermob-proxy to capture network traffic and extract the .m3u8 link dynamically.

import os
import subprocess
from selenium import webdriver
from browsermobproxy import Server
import time

def start_proxy():
    # Path to browsermob-proxy (download and set the path)
    server_path = "/path/to/browsermob-proxy"
    server = Server(server_path)
    server.start()
    proxy = server.create_proxy()
    return server, proxy

def setup_webdriver(proxy):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument(f"--proxy-server={proxy.proxy}")
    driver = webdriver.Chrome(options=chrome_options)
    return driver

def fetch_m3u8_url(page_url):
    server, proxy = start_proxy()
    proxy.new_har("video_page", options={"captureHeaders": True, "captureContent": True})

    driver = setup_webdriver(proxy)

    try:
        # Load the video page
        driver.get(page_url)
        time.sleep(10)  # Allow time for network traffic to load

        # Analyze the network logs to find .m3u8
        har_data = proxy.har
        for entry in har_data["log"]["entries"]:
            url = entry["request"]["url"]
            if ".m3u8" in url:
                return url

    finally:
        driver.quit()
        server.stop()

def download_hls_stream(m3u8_url, output_path):
    try:
        subprocess.run(
            ["ffmpeg", "-i", m3u8_url, "-c", "copy", output_path],
            check=True
        )
        print(f"Download complete. File saved to {output_path}.")
    except subprocess.CalledProcessError as e:
        print(f"Error downloading video: {e}")

if __name__ == "__main__":
    # Replace with the video page URL
    video_page_url = "https://xxxxx.mediaspace.kaltura.com/media/x%xxxxx%xxxxxxx:%xx%20KBYG/1_mavz1jq4/xxxxx"

    # Define the output file path
    output_path = os.path.expanduser("~/Downloads/video.mp4")

    # Fetch the .m3u8 URL
    print("Fetching .m3u8 URL...")
    m3u8_url = fetch_m3u8_url(video_page_url)
    if m3u8_url:
        print(f"Found .m3u8 URL: {m3u8_url}")

        # Download the video
        print("Downloading video...")
        download_hls_stream(m3u8_url, output_path)
    else:
        print("Failed to find .m3u8 URL.")
  1. Steps to Set Up and Run

  2. Download BrowserMob Proxy:

  • Download BrowserMob Proxy and extract it.
  • Replace /path/to/browsermob-proxy with the actual path to the browsermob-proxy binary.

2.Install WebDriver:

  • Install the ChromeDriver that matches your Chrome version.
  • Ensure the driver is in your PATH or specify its location when initializing webdriver.Chrome().

3.Run the Script:

  • Save the script as fetch_and_download.py.
  • Execute it:

OR

Could for example start with a Chrome extension to automate the process of finding the .m3u8 URL and trigger the python script in the background (with notification)

links

social