Black Synology generates video thumbnails with one click

question

When we use VideoStation, if the upload is not a film and television drama, etc., there is no way to automatically find the thumbnail of the poster, so we made a tutorial here. Use FFmpeg , Python respectively

solution

1. Enter the Synology system
[the yellow highlighter marks the place that needs to be clicked]
insert image description here

2. Add the source
insert image description here
name of the third-party package with the same name as the location.
insert image description here
Location URL:
[DSM6.x version]

https://spk.imnks.com/

[DSM7.x version]

https://spk7.imnks.com/

3. To install the software package
, click the community
to search for ffmpeg and install it.
insert image description here
4. Enable the Synology SSH function.
Please see the following figure for the operation steps. 5.
insert image description here
Use the SSH tool to connect
to Synology . Account Enter the password of the group management account and log in successfully 6. Add Python files and copy all
insert image description here

insert image description here

insert image description here

insert image description here

insert image description here

#!/usr/bin/python
# -*- coding:UTF-8 -*-
import os
import sys


def check_type(filename,video_list):
    """
    检查是否为视频文件
    """
    array = map(filename.endswith,video_list)
    if True in array:
        return True
    else:
        return False


def get_capture_delay_time(file_path):
    """
    获取截取图片在视频中位置
    """
    if not os.path.exists(file_path):
        return False
    file_size = os.path.getsize(file_path)
    if file_size <= 1 * 1024 * 1024:                                # 视频大小 <= 1MB,截取视频第1S图片
        delay_time = 1
    elif file_size <= 4 * 1024 * 1024:                              # 1MB < 视频大小 <= 4MB,截取视频第5S图片
        delay_time = 5
    elif file_size <= 50 * 1024 * 1024:                             # 4MB < 视频大小 <= 50MB,截取视频第10S图片
        delay_time = 10
    else:
        delay_time = 20                                             # 50MB < 视频大小,截取视频第20S图片
    return delay_time


def check_file_existed(file_path):
    """
    检查文件是否存在
    """
    if os.path.exists(file_path):
        return True
    else:
        return False


def get_file_pre(file_name):
    """
    获取文件名前缀,如my_video.mp4,返回my_video
    """
    video_name_list = os.path.splitext(file_name)
    if len(video_name_list) == 2:
        video_name_pre = video_name_list[0]
    else:
        video_name_pre = False
    return video_name_pre


def make_thumb(file_path,video_types):
    """
    遍历文件夹,调用ffmpeg截取视频图片
    """
    for dir_path,dir_names,file_names in os.walk(file_path):
        for name in file_names:
            # print("*"*20)
            # print(name)
            if not check_type(name,video_types):                    # 不是视频文件,跳过本地迭代,继续下一次迭代
                print("not video,continue next")
                continue
            video_full_path = os.path.join(dir_path, name)
            video_name_pre = get_file_pre(name)
            if video_name_pre:                                      # 如果缩略图已经存在,跳过本地迭代,继续下一次迭代
                pic_name = '%s%s' % (video_name_pre,'.jpg')
                picture_full_path = os.path.join(dir_path,pic_name)
                ret = check_file_existed(picture_full_path)
                if ret:
                    print("%s existed,continue next" % picture_full_path)
                    continue
                delay_time = get_capture_delay_time(video_full_path)
                if not delay_time:                                  # 获取文件大小失败,跳过本地迭代,继续下一次迭代
                    continue
                shell = 'ffmpeg -i "%s" -y -ss %s -frames:v 1 "%s"' % \
                        (video_full_path, delay_time,picture_full_path)
                # print("#"*20)
                print(shell)
                os.system(shell)
                print("%s capture success" % picture_full_path)


if __name__ == "__main__":
    # 当前路径中执行脚本
    file_path = sys.path[0]
    video_types = ['.mp4','.avi','.wmv','.mkv','.flv']
    make_thumb(file_path,video_types)

Enter sudo -iand then enter the password of the administrator
to ls /
enter to find your own video storage location, method: the folders starting with volume are all hard disks, ls /volume a few times, you can find the location where the video is stored
insert image description here
, confirm the location and inputcd 目录

Enter vi 1.py,
insert image description here
press Enter, press i , right-click and copy a lot of code just copied into the terminal,
insert image description here
press ESC , enter : wq, and press Enter. Note that it is the English
insert image description here
SSH software download location to
download [the second half of SSH, some people may I don’t understand, the general meaning is to put this Python file under the video directory where you want to generate thumbnails. The vi used is equivalent to the command to modify the notepad. If you don’t understand, you can find a tutorial about vi on the Internet ]
to proceed python2 1.pyExecute to generate thumbnails

Guess you like

Origin blog.csdn.net/qq_36678880/article/details/124934495