You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
5.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# pip uninstall -y Pillow==9.5.0
# pip install Pillow==9.5.0
import os
from urllib.parse import unquote
import uuid
import shutil
import subprocess
import pkg_resources
import sys
def install(package):
subprocess.check_call(
[sys.executable, "-m", "pip", "install", package, "-i", "https://pypi.tuna.tsinghua.edu.cn/simple"])
if __name__ == "__main__":
# 安装ffmpeg
try:
# 尝试导入包
pkg_resources.get_distribution('ffmpeg')
except pkg_resources.DistributionNotFound:
# 如果未安装使用pip安装ffmpeg
print("ffmpeg库未安装正在安装...")
install('ffmpeg-python ')
finally:
# 包安装后或者已存在时,导入包
import ffmpeg
print("ffmpeg库已加载。")
# 安装Pillow
try:
# 尝试导入包
pkg_resources.get_distribution('Pillow')
except pkg_resources.DistributionNotFound:
# 如果未安装使用pip安装ffmpeg
print("Pillow库未安装正在安装...")
install('Pillow==9.5.0')
finally:
# 包安装后或者已存在时,导入包
from PIL import Image
print("Pillow库已加载。")
# 创建目录
directory = "assets/images/"
if os.path.exists("target.txt"):
os.remove("target.txt")
# 检查目录是否存在
if os.path.exists(directory):
# 删除目录及其所有内容
shutil.rmtree(directory)
os.makedirs(directory)
# 打开文件
with open('source.txt', 'r', encoding='utf-8') as file:
# 逐行读取文件内容
for line in file:
# 每一行
video_path = line.strip()
# 获取信息
probe = ffmpeg.probe(video_path)
# print('source_video_path: {}'.format(video_path))
format = probe['format']
bit_rate = int(format['bit_rate']) / 1000
size = int(format['size']) / 1024 / 1024
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
if video_stream is None:
print('No video stream found!')
continue
width = int(video_stream['width'])
height = int(video_stream['height'])
num_frames = int(video_stream['nb_frames'])
fps = int(video_stream['r_frame_rate'].split('/')[0]) / int(video_stream['r_frame_rate'].split('/')[1])
duration = float(video_stream['duration'])
# Convert size to MB
video_size = "{:.1f}MB".format(size)
video_size = video_size.replace("M", "")
# print(video_size)
# Convert duration to minutes and seconds
minutes = int(duration // 60)
seconds = int(duration % 60)
# Format minutes and seconds with leading zeros if necessary
minutes_str = str(minutes).zfill(2)
seconds_str = str(seconds).zfill(2)
video_length = "{}:{}".format(minutes_str, seconds_str)
# print(video_length)
# 获取中文名称
# 使用字符串分割方法,获取最后一个'/'后面的部分
last_part = video_path.split('/')[-1]
# 对URL编码的字符串进行解码
video_name = unquote(last_part)
# 设置输出图片路径
# 生成一个随机的 UUID
new_uuid = uuid.uuid4()
# 将 UUID 对象转换为字符串类型
uuid_string = str(new_uuid)
video_thumb = directory + uuid_string + '.jpg'
if os.path.exists(video_thumb):
os.remove(video_thumb)
# 使用 ffmpeg-python 进行视频截取
ffmpeg.input(video_path).filter('select', 'gte(n,0)').output(video_thumb, vframes=1).run()
# 打开原始图像
image = Image.open(video_thumb)
# 获取原始图像的宽度和高度
width, height = image.size
# 计算等比例缩放后的高度
new_height = int((320 / width) * height)
# 缩放图像
resized_image = image.resize((320, new_height), Image.ANTIALIAS)
# 保存缩放后的图像
resized_image.save(video_thumb)
# 关闭原始图像
image.close()
# 读入模板文件
with open('Template.html', 'r', encoding='utf-8') as tFile:
# 读取文件的全部内容
content = tFile.read()
content = content.replace("{{video_url}}", video_path)
content = content.replace("{{video_size}}", video_size)
content = content.replace("{{video_length}}", video_length)
content = content.replace("{{video_thumb}}", video_thumb)
content = content.replace("{{video_name}}", video_name.replace(".mp4",""))
# 打开文件并设置为追加模式
with open('target.txt', 'a', encoding='utf-8') as fw:
# 向文件追加内容
fw.write(content + "\n")
print("恭喜,所有操作成功完成!")