Python小脚本

这篇博客介绍了如何使用Python进行音视频处理,首先确保安装了相关库,通过命令行进行安装,接着展示了作者下载的截图,暗示将分享具体操作或示例。
摘要由CSDN通过智能技术生成

import datetime
import json
import os
import random
import requests
import concurrent.futures

def extract_links_from_har_file(har_file_path, url_prefix):
    with open(har_file_path, 'r', encoding='utf-8') as har_file:
        har_data = json.load(har_file)

    entries = har_data['log']['entries']
    video_links = []
    for entry in entries:
        request_url = entry['request']['url']
        if request_url.startswith(url_prefix):
            video_links.append(request_url)

    return video_links

def download_video(video_url, output_folder):
    try:
        response = requests.get(video_url, stream=True)
        if response.status_code == 200:
            video_filename = os.path.join(output_folder, generate_unique_filename())
            with open(video_filename, 'wb') as file:
                for chunk in response.iter_content(chunk_size=8192):
                    file.write(chunk)
            print(f"Downloaded: {video_url}")
        else:
            print(f"Failed to download: {video_url}")
    except Exception as e:
        print(f"Error downloading {video_url}: {str(e)}")

def generate_unique_filename():
    random_num = random.randint(1000, 9999)
    current_date = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    return f"video_{current_date}_{random_num}.mp4"

def download_videos(video_links, output_folder, num_threads=35):
    with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
        futures = [executor.submit(download_video, video_url, output_folder) for video_url in video_links]

        for future in concurrent.futures.as_completed(futures):
            try:
                future.result()
            except Exception as e:
                print(f"Error: {str(e)}")

if __name__ == "__main__":
    har_file_path = r"你的har文件位置"
    url_prefix = "你的请求的域名"
    video_links = extract_links_from_har_file(har_file_path, url_prefix)
    print(video_links)

    output_folder = r"你的下载视频保存的位置"

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    download_videos(video_links, output_folder)

确保已经安装了 requests。如果你没有安装它,你可以使用以下命令来安装:

pip install requests

这是我下载的截图

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello_中年人

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值