How to Download Amazon Product Videos (m3u8) with Python – Full Code Tutorial

This article walks through a Python‑based solution for extracting Amazon product videos stored as m3u8 streams, explains the format, shows how to retrieve the .ts segments, combine them into an MP4 file, and offers alternative ffmpeg commands for quick downloading.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Download Amazon Product Videos (m3u8) with Python – Full Code Tutorial

Introduction

The author, a Python enthusiast, shares a question from a community member about scraping Amazon product videos and provides a step‑by‑step solution.

Solution Process

Using browser network inspection, the video was identified as an m3u8 stream – Apple’s HTTP Live Streaming format encoded in UTF‑8. Once the m3u8 URL is obtained, the video can be downloaded.

The following GitHub link contains a ready‑made script that the author adopts:

https://github.com/PY-GZKY/python-automation-docs/blob/master/docs/%E7%88%AC%E8%99%AB/m3u8%E9%9F%B3%E8%A7%86%E9%A2%91%E6%8B%BC%E6%8E%A5.md

Core Python code:

import datetime
import time
import os
import requests

# m3u8 is the local file path

def get_ts_urls(m3u8_path):
    urls = []
    with open(m3u8_path, "r") as file:
        lines = file.readlines()
        for line in lines:
            if line.endswith(".ts
"):
                print(line)
                urls.append("https://m.media-amazon.com/images/S/vse-vms-transcoding-artifact-us-east-1-prod/084028a2-9a64-485f-a55b-676577059927/" + line.strip("
"))
    return urls

def download(ts_urls, download_path):
    for i in range(len(ts_urls)):
        ts_url = ts_urls[i]
        file_name = ts_url.split("/")[-1]
        print("开始下载 %s" % file_name)
        try:
            response = requests.get(ts_url, stream=True, verify=False)
        except Exception as e:
            print("异常请求:%s" % e.args)
            return
        ts_path = download_path + "/{0}.ts".format(i)
        with open(ts_path, "wb+") as file:
            for chunk in response.iter_content(chunk_size=1024):
                if chunk:
                    file.write(chunk)
        time.sleep(.56)

def file_walker(path):
    file_list = os.listdir(path)
    file_list.sort(key=lambda x: int(x[:-3]))
    file_list_ = []
    for fn in file_list:
        p = "tsfiles/" + fn
        file_list_.append(p)
    print(file_list_)
    return file_list_

def combine(ts_path, file_name):
    file_list = file_walker(ts_path)
    file_path = file_name + '.MP4'
    with open(file_path, 'wb+') as fw:
        for i in range(len(file_list)):
            fw.write(open(file_list[i], 'rb').read())

if __name__ == "__main__":
    urls = get_ts_urls("./data.txt")
    download(urls, "tsfiles")
    combine("tsfiles", "大理")

The script downloads each ts segment, saves them locally, and then merges them into a single mp4 file using the combine() function.

An alternative method mentioned is using ffmpeg directly:

ffmpeg -allowed_extensions ALL -i <m3u8_url> -c copy output_video.mp4

Conclusion

The tutorial demonstrates how to locate the m3u8 URL, download the constituent .ts files with Python, and assemble them into a playable video, while also offering a quick ffmpeg command for those who prefer a one‑liner solution.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ffmpegVideo Downloadm3u8web-scrapingcode-tutorial
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.