Download Amazon Product Videos with Python: A Complete m3u8 Scraping Guide

This tutorial explains how to extract and download Amazon product videos by capturing m3u8 streams with Python, providing step‑by‑step code to retrieve .ts segments, merge them into an MP4 file, and alternative ffmpeg commands for quick conversion.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Download Amazon Product Videos with Python: A Complete m3u8 Scraping Guide

1. Introduction

A user in a Python community asked how to scrape Amazon product videos. This article shares a solution using Python to download the m3u8 video streams.

2. Solution Process

The video is delivered in m3u8 format, a variant of Apple’s HLS streaming protocol encoded in UTF‑8. By extracting the .ts segment URLs from the m3u8 file, each segment can be downloaded and later merged into a single MP4 file.

The following code (adapted from a GitHub tutorial) demonstrates the complete workflow:

import datetime
import time
import os
import requests

# m3u8 is a 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 = str("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 using the combine() function to produce a final .mp4 file.

Alternatively, the ffmpeg tool can be used with a single command:

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

3. Conclusion

The guide provides a practical method for downloading m3u8 videos with Python, offering both a custom script and a quick ffmpeg shortcut for efficient video extraction.

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.

AutomationffmpegVideo Downloadm3u8
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.