下载视频并且直接上传到服务器上

下载视频并且直接上传到服务器上

从网络上批量采集视频下载到本地, 并且直接通过sftp上传到服务器上;

# -*- coding: utf-8 -*-
import os
import time
from concurrent.futures import ThreadPoolExecutor

import paramiko
from yt_dlp import YoutubeDL


# AI 视频换脸!史上最强最快的方法,100%成功,完全免费,无需配置,打开即可使用!|  零度解说 [nSDz2W7SbOk].mp4
# AI 视频换脸!史上最强最快的方法,100%成功,完全免费,无需配置,打开即可使用!|  零度解说[nSDz2W7SbOk].mp4

# 【火爆全网的】AI视频生成神器!让老外说中文,口型自然,不限语言,多用途,还不快来试试! | 零度解说 [N3CTgtz3GMc].mp4
# 【火爆全网的】AI视频生成神器!让老外说中文,口型自然,不限语言,多用途,还不快来试试! | 零度解说 [N3CTgtz3GMc].mp4

def youtube_download(url):
    """
    cachedir: 禁用缓存
    quiet: 不打印
    extract_flat: 如果是播放列表,仅返回视频列表,不解析每个视频的下载地址
    """
    ydl = YoutubeDL({'cachedir': False, 'quiet': True, 'extract_flat': True, })
    result = ydl.extract_info(url, download=True)
    return result


def upload_video(response):
    """
    上传视频到服务器;
    :param local_path:
    :param remote_path:
    :return:
    """
    # title = response.result().get("title") + " " + f'[{response.result().get("id")}]' + '.mp4'
    title = response.result().get("requested_downloads")[0].get("_filename")
    hostname = '192.168.177.129'
    port = 22
    username = 'whj'
    password = '123456'
    # 创建SSH客户端
    client = paramiko.SSHClient()

    # 允许连接不在known_hosts文件中的主机
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # 连接服务器
    client.connect(hostname, port=port, username=username, password=password)

    # 创建SFTP客户端
    sftp = client.open_sftp()
    v = r"D:\code\pythoncode\pro_video_utils\scripts"
    local_path = os.path.join(v, title)
    # print(local_path)
    remote_path = '/home/whj/data/video/' + title
    # 上传文件
    sftp.put(local_path, remote_path)
    os.remove(local_path)

    # 关闭SFTP客户端
    sftp.close()

    # 关闭SSH连接
    client.close()

    print("文件上传成功!")
    return True


if __name__ == '__main__':
    pool = ThreadPoolExecutor(10)
    li = [
        "https://www.youtube.com/watch?v=uSVlBs8LB00",
        "https://www.youtube.com/watch?v=nSDz2W7SbOk",
        "https://www.youtube.com/watch?v=emK7Me-Mldo",
        "https://www.youtube.com/watch?v=N3CTgtz3GMc"
    ]

    start = time.time()
    for href in li:
        fur = pool.submit(youtube_download, href)  # 异步多线程
        fur.add_done_callback(upload_video)
    pool.shutdown(True)
    end = time.time()  # 使用多线程, 可以缩短一半的时间
    print("运行时间:", end - start)

继续努力,终成大器;

posted @ 2024-01-28 21:43  紫青宝剑  阅读(45)  评论(0编辑  收藏  举报