批量获取API接口视频类并下载 带MD5校验去重(Python)

简介

此款脚本适用于访问API直接返回视频的网站, 如api接口返回的为json格式,可联系站长进行改写

批量获取API接口视频类并下载 带MD5校验去重(Python)插图

import requests
import os
from tqdm import tqdm
import hashlib

url = "https://v1.21lhz.cn/API/Video/api.php"
save_path = "/home/video/"

def download_videos(url, save_path, reset_times):
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    
    for i in range(reset_times):
        response = requests.get(url, stream=True)
        
        if response.status_code == 200:
            total_size = int(response.headers.get('Content-Length', 0))
            video_name = f"video_{i+1}.mp4"  # 根据实际情况给视频命名
            video_path = os.path.join(save_path, video_name)
            
            with open(video_path, "wb") as f, tqdm(
                    total=total_size, unit='B', unit_scale=True, desc=f"下载 {video_name}"
            ) as pbar:
                for chunk in response.iter_content(chunk_size=1024):
                    size = f.write(chunk)
                    pbar.update(size)
            
            print(f"视频 {video_name} 下载完成.")
        
        else:
            print("请求失败.")
    
    deduplicate_videos(save_path)

def calculate_md5(file_path):
    with open(file_path, 'rb') as f:
        data = f.read()
        md5_value = hashlib.md5(data).hexdigest()
    return md5_value

def deduplicate_videos(directory):
    video_files = os.listdir(directory)
    video_md5_dict = {}
    
    for video_file in video_files:
        video_path = os.path.join(directory, video_file)
        video_md5 = calculate_md5(video_path)
        
        if video_md5 not in video_md5_dict:
            video_md5_dict[video_md5] = video_path
        else:
            print(f"视频 {video_file} 与 {video_md5_dict[video_md5]} 重复,已删除.")
            os.remove(video_path)

if __name__ == "__main__":
    reset_times = int(input("请输入要重置访问多少次:"))
    download_videos(url, save_path, reset_times)

 

阅读剩余
THE END
诺言博客