Python基于FFMPEG自动截图上传至imgbox(四)

本文最后更新于:2 年前

前言


话接上回,我们已经实现了从视频到截图到bbcode的过程,但是还是有一个细节,很多网站短mediainfo没法弄,那么希望能够顺带也获取一下。

核心代码及思想


这次使用到的就是mediainfo了,这个在linux可以用 apt install mediainfo 安装,windows下可以下载一个cli版添加到环境变量。

教程:https://fossies.org/linux/MediaInfo_CLI/MediaInfo/Contrib/CLI_Help.doc

上面放了一个doc文档,可以自定义怎么输出剪短的mediainfo,但是我们现在就只需要长的就好了。话不多说,上代码:

import subprocess

def get_mediainfo(file) -> str:
    process = subprocess.Popen(["mediainfo", file], stdout=subprocess.PIPE)
    output, error = process.communicate()

    if not error and output != b"\n":
        output = output.decode()  # bytes -> string
        output = re.sub(re.escape(file), os.path.basename(file), output)  # Hide file path
        return output
    else:
        return ''

简而言之就是通过调用mediainfo命令获取到了想要的信息并且只保留视频文件名。

接下来就剩最后一个问题了,怎么快速获取到视频的存储路径呢?这里仅仅适合于qbittorrent,其他客户端应该也有类似的,先不探讨了。这里登场的模块叫做qbittorrent-api,用于调用qbittorrent的web api达到一些想要的结果。话不多说,上代码:

import qbittorrentapi
import os

qb_host = "http://localhost:2021" # 这里基本上部署在本地才可以截图所以就直接用localhost了,假设端口是2021
qb_name = "admin"
qb_pwd = "123456"

def get_save_path_from_hash(hash_value):
    # 首先登陆qbittorrent之前需要开启webui,然后通过host, port(可选), name, password进行登录
    qb = qbittorrentapi.Client(host=qbt_address, port=qbt_port, username=qbt_user, password=qbt_password)
    try:
    	qb.auth_log_in()
        torrents = qb.torrents_info(hashes=hash_value) # 基本上有1个或者没有,返回种子列表
        torrent_name = torrents[0]['name']
        save_path = torrents[0]['save_path'] # 获取到save_path和name基本上就可以得到视频或者视频文件的目录了
        path = os.path.join(save_path, torrent_name) # 对path进行扫描获取到视频文件路径即可
        ...
    except Exception as exc:
        print(exc)

qbittorrentapi还有很多其他的命令,这里只是用到了查询,还可以增删改,此处应有掌声。

到了这一步,其实已经算完结了。整合所有的代码就可以完成从hash到视频mediainfo到截图到上传。怎么样,应该是学会了吧?不会也不要紧,直接上链接。

https://github.com/tomorrow505/qbittorrent433/blob/main/get_mediaifno_picture.py

如果你想在盒子上部署,也可以用下面的命令一键部署:

wget https://raw.githubusercontent.com/tomorrow505/qbittorrent433/main/get_mediaifno_picture.sh -O get_mediaifno_picture.sh && /bin/bash get_mediaifno_picture.sh

运行命令以后就可以用 up hash_of_torrent来进行传图了。

总结


到了这一步,其实传图命令已经差不多了,Windows、linux下都可以愉快的截图上传了。

那么这一系列的教程就到此结束了,谢谢!!



本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!