分享一个使用you-get批量下载多个视频合集的Python脚本

使用方法

  1. collections定义成自己要下载的合集列表
  2. 终端执行 python download_collections.py
  3. 完成后,每个合集的已下载文件数量会记录在download_log.log中

运行结果​​​​​​​​​​​​​​

源码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import subprocess
import os
import logging
import shutil

# 配置日志记录器
logging.basicConfig(filename='download_log.log', level=logging.INFO, format='%(asctime)s - %(message)s')

# 定义合集信息列表,每个元素是一个包含合集名和网址的字典
collections = [
    {
        '合集名': 'collections-1',
        '网址': 'url-1 替换成实际的url'
    },
    {
        '合集名': 'collections-2',
        '网址': 'url-2 替换成实际的url'
    }
    # 可以继续添加更多的合集信息
]

# 遍历合集信息列表并执行下载操作
for collection in collections:
    collection_name = collection['合集名']
    url = collection['网址']

    # 删除已存在的collection_name目录
    folder_path = os.path.join(os.getcwd(), collection_name)
    if os.path.exists(folder_path):
        shutil.rmtree(folder_path)

    # 构建you-get命令
    command = f'you-get -c cookies.sqlite --no-caption -l -o {collection_name} {url}'

    # 执行命令
    try:
        subprocess.run(command, shell=True, check=True)
        print(f'成功下载合集: {collection_name}')

        # 检查文件夹中的文件数量
        file_count = len([f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))])
        print(f'{collection_name} 文件数量: {file_count}')

        # 将文件数信息写入日志文件
        logging.info(f'{collection_name} 已下载文件数量: {file_count}')

    except subprocess.CalledProcessError as e:
        print(f'下载合集 {collection_name} 时出错: {e}')

print('所有合集下载完成。')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值