群晖ffmpeg影片截图脚本

我们经常会有各种影片,没有预览图,封面图,等等比如欧美大姐姐自制视频.这个时候怎么办呢?

我自己写了一个python脚本来自动截图

1.在群晖中添加第三方源:https://packages.synocommunity.com/

2.在源中找到ffmpeg 安装!

3.用新的源替换ffmpeg

mv /usr/bin/ffmpeg /usr/bin/ffmpeg_bak
cp -r /usr/local/ffmpeg/bin/ffmpeg /usr/bin/ffmpeg

4.安装python3,安装源中的python3

如果你不想每次都输入地址可以使用

cp -r /usr/local/python3/bin/python3  /usr/bin/python3

5.复制粘贴我的截图I脚本

#!/usr/bin/python
# -*- coding:UTF-8 -*-
import os
import sys
from PIL import Image
path = sys.path[0]
def exte(a, *endstring):
    array = map(a.endswith, endstring)
    if True in array:
        return True
    else:
        return False
def make_thumb():
    for dirpath, dirnames, filenames in os.walk(path):
        if dirpath.find("写真") < 0 and dirpath.find("系列") < 0:
            for name in filenames:
                if exte(name, type):
                    try:
                        videopath = os.path.join(dirpath, name)
                        videoname = os.path.splitext(name)[0]
                        outputname = os.path.join(dirpath, videoname)
                        outputname2 = os.path.join(dirpath, videoname+".jpg")
                        if not os.path.exists(outputname2):
                            time = os.popen(
                                'ffmpeg -i %s 2>&1 | grep "Duration" | cut -d " " -f 4 | sed s/,//' % (videopath)).read().strip()
                            timeArr = time.split(":")
                            showtime = int(
                                (float(timeArr[0])*3600)+(float(timeArr[1])*60)+(float(timeArr[2])*1)-20)
                            num = 4
                            row = 2
                            line = 2
                            if showtime > 3600 and showtime < 5400:
                                num = 6
                                row = 2
                                line = 3
                            elif showtime >= 5400:
                                num = 9
                                row = 3
                                line = 3
                            elif showtime < 1:
                                print("short_"+str(time)+"_"+str(outputname2))
                                continue
                            showtime2 = int(showtime/num+1)
                            nowtime = 20
                            for i in range(row):
                                for j in range(line):
                                    shell = 'ffmpeg -y -ss %s -i %s -f image2 -vf "scale=640:360:force_original_aspect_ratio=decrease,pad=640:360:(ow-iw)/2:(oh-ih)/2" -frames 1 %s_img_%s_%s.jpg' % (
                                        nowtime, videopath, outputname, i, j)
                                    os.system(shell)
                                    nowtime = nowtime + showtime2
                            print(str(time)+"_"+str(showtime2)+"_"+str(num))
                            toImage = Image.new('RGB', (1280, 640))
                            if num == 6:
                                toImage = Image.new('RGB', (1920, 640))
                            elif num == 9:
                                toImage = Image.new('RGB', (1920, 1080))
                            for i in range(row):
                                
                                for j in range(line):
                                    # 每次打开图片绝对路路径列表的第一张图片
                                    pic_fole_head = Image.open('%s_img_%s_%s.jpg' % (outputname, i, j))
                                    # 计算每个图片的左上角的坐标点(0, 0),(0, 200),(0, 400),(200, 0),(200, 200)。。。。(400, 400)
                                    toImage.paste(pic_fole_head, box=(j * 640, i * 360))
                                    
                            toImage.save('%s.jpg' % (outputname))
                            for i in range(row):
                                for j in range(line):
                                    os.remove('%s_img_%s_%s.jpg' %(outputname, i, j))
                    except Exception as result:
                        print('未知错误: %s' % result)
    return
types = ['.mp4', '.avi', '.wmv', '.mkv', '.flv', '.ts', '.mov', '.m4v']
for type in types:
    make_thumb()

5.在吧这个python内容,复制到 你需要截图的视频的根目录,新建一个 jietu.py 会自动遍历所有子目录.并且在视频的位置,生成和视频文件同名的 .JPG文件.

6.在群晖中添加计划任务 python3 /web/jietu.py

发表评论