使用pyscenedetect进行视频场景切割

1. 简介

在视频剪辑有转场一词:一个视频场景转换到另一个视频场景,场景与场景之间的过渡或转换,就叫做转场。
本篇介绍一个强大的开源工具PySceneDetect,它是一款基于opencv的视频场景切换检测和分析工具,项目地址: https://github.com/Breakthrough/PySceneDetect

2. 创建使用环境

conda create -n pyscenedetect python=3.7
conda activate pyscenedetect
conda install ffmpeg -y
pip install scenedetect opencv-python

3. 命令行测试

pyscenedetect提供了一个命令行工具,可以通过-h参数来查看它的帮助信息

Usage: scenedetect [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...For example:scenedetect -i video.mp4 -s video.stats.csv detect-content list-scenesNote that the following options represent [OPTIONS] above. To list theoptional [ARGS] for a particular COMMAND, type `scenedetect help COMMAND`.You can also combine commands (e.g. scenedetect [...] detect-content save-images --png split-video).Options:-i, --input VIDEO             [Required] Input video file. May be specifiedmultiple times to concatenate several videostogether. Also supports image sequences andURLs.-o, --output DIR              Output directory for all files (stats file,output videos, images, log files, etc...).-f, --framerate FPS           Force framerate, in frames/sec (e.g. -f29.97). Disables check to ensure that allinput videos have the same framerates.-d, --downscale N             Integer factor to downscale frames by (e.g. 2,3, 4...), where the frame is scaled to width/Nx height/N (thus -d 1 implies no downscaling).Each increment speeds up processing by afactor of 4 (e.g. -d 2 is 4 times quicker than-d 1). Higher values can be used for highdefinition content with minimal effect onaccuracy. [default: 2 for SD, 4 for 720p, 6for 1080p, 12 for 4k]-fs, --frame-skip N           Skips N frames during processing (-fs 1 skipsevery other frame, processing 50% of thevideo, -fs 2 processes 33% of the frames, -fs3 processes 25%, etc...). Reduces processingspeed at expense of accuracy.  [default: 0]-m, --min-scene-len TIMECODE  Minimum size/length of any scene. TIMECODE canbe specified as exact number of frames, a timein seconds followed by s, or a timecode in theformat HH:MM:SS or HH:MM:SS.nnn  [default:0.6s]--drop-short-scenes           Drop scenes shorter than `--min-scene-len`instead of combining them with neighbors-s, --stats CSV               Path to stats file (.csv) for writing framemetrics to. If the file exists, any metricswill be processed, otherwise a new file willbe created. Can be used to determine optimalvalues for various scene detector options, andto cache frame calculations in order to speedup multiple detection runs.-v, --verbosity LEVEL         Level of debug/info/error information to show.Setting to none will suppress all outputexcept that generated by actions (e.g.timecode list output). Can be overriden by`-q`/`--quiet`.-l, --logfile LOG             Path to log file for writing applicationlogging information, mainly for debugging.Make sure to set `-v debug` as well if you aresubmitting a bug report.-q, --quiet                   Suppresses all output of PySceneDetect exceptfor those from the specified commands.Equivalent to setting `--verbosity none`.Overrides the current verbosity level, even if`-v`/`--verbosity` is set.-h, --help                    Show this message and exit.Commands:about             Print license/copyright info.detect-content    Perform content detection algorithm on input video(s).detect-threshold  Perform threshold detection algorithm on input video(s).export-html       Exports scene list to a HTML file.help              Print help for command (help [command]).list-scenes       Prints scene list and outputs to a CSV file.save-images       Create images for each detected scene.split-video       Split input video(s) using ffmpeg or mkvmerge.time              Set start/end/duration of input video(s).version           Print version of PySceneDetect.

找个包含多场景切换的视频测试一下,执行命令

scenedetect -i lldq.mp4 detect-content split-video

脚本运行结束后,会在当前目录生成每个镜头的视频片段,每个视频片段只包含一个场景:
在这里插入图片描述

如果想从视频的某个时间点开始,可以使用参数time:

scenedetect -i lldq.mp4 time -s 5s detect-content split-video

还可以将检测后的场景图片保存下来,同时生成统计文件csv:

scenedetect.exe -i lldq.mp4 -o video_scenes detect-content save-images

4. 场景切割算法

pyscenedetect使用了2种场景切割的方法,它们是detect-content和detect-threshold,除此之外,它还支持自定义检测算法。

  • detect-content
    顾名思义,这种方法就是根据前后图像的内容来进行判断,与我们常识中所说的视频转场是一样的。算法会根据前后2帧的视频数据,计算出它们不同的区域大小,如果这个区域大于某个预先设定的值(默认是30,可以通过–threshold参数来指定),那么就认为场景已经切换了
  • detect-threshold
    这是比较传统的检测方法,有点像ffmpeg中的blackframe滤镜。它会用特定的值去跟数据帧的亮度比较进行,如果大于某个预先设定的值,就认为场景已经切换了。在pyscenedetect中,这个值是由视频帧的每个像素的RGB的平均值计算而来
  • 自定义检测算法
    所有的检测算法必须继承自SceneDetector这个类
from scenedetect.scene_detector import SceneDetectorclass CustomDetector(SceneDetector):"""CustomDetector class to implement a scene detection algorithm."""def __init__(self):passdef process_frame(self, frame_num, frame_img, frame_metrics, scene_list):"""Computes/stores metrics and detects any scene changes.Prototype method, no actual detection."""returndef post_process(self, scene_list):pass

类中主要有2个方法,process_frame负责处理所有的视频帧;post_process是可选的,它在process_frame结束后执行,主要用来做一些后期处理,比如场景切换数据的文件保存。

下面主要来看看process_frame方法,它有如下几个重要参数

更加实现细节方面,可以参考源码目录下的scenedetect/detectors/content_detector.py或scenedetect/detectors/threshold_detector.py

  • frame_num: 当前处理到的帧数
  • frame_img: 返回的帧数据,格式是numpy数组
  • frame_metrics: 保存检测算法计算结果的字典
  • scene_list: 视频中所有场景切换包含的帧数列表

5. Python API的使用

如果需要在自己的代码中去使用pyscenedetect,除了使用命令行调用的方式外,pyscenedetect还提供了基于python的API。

下面是一个简单的demo,程序读取视频文件,使用content-detector算法进行检测,最后将所有场景的开始时间、结束时间和总的帧数分别打印输出。

from scenedetect.video_manager import VideoManager
from scenedetect.scene_manager import SceneManager
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors.content_detector import ContentDetectordef find_scenes(video_path):video_manager = VideoManager([video_path])stats_manager = StatsManager()scene_manager = SceneManager(stats_manager)# 使用contect-detectorscene_manager.add_detector(ContentDetector())try:video_manager.set_downscale_factor()video_manager.start()scene_manager.detect_scenes(frame_source=video_manager)scene_list = scene_manager.get_scene_list()print('List of scenes obtained:')for i, scene in enumerate(scene_list):print('Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (i + 1,scene[0].get_timecode(), scene[0].get_frames(),scene[1].get_timecode(), scene[1].get_frames(),))finally:video_manager.release()if __name__ == '__main__':find_scenes('lldq.mp4')

运行输出如下:
在这里插入图片描述

6. 参考

https://github.com/Breakthrough/PySceneDetect
https://pyscenedetect.readthedocs.io/projects/Manual/en/latest/
https://blog.gdeltproject.org/using-ffmpegs-blackdetect-filter-to-identify-commercial-blocks/
https://blog.csdn.net/djstavaV/article/details/118215641
https://blog.csdn.net/daydayup858/article/details/128256460
http://scenedetect.com/projects/Manual/en/latest/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/214259.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在做题中学习(31):电话号码的字母组合(全排列)

17. 电话号码的字母组合 - 力扣(LeetCode) 思路:既然要排列组合,就得先根据数字字符取出来 所以先定义一个string类的数组通过下标取到每个数字对应的映射。 string _numsTostr[10]{"","","abc"…

Android:java.lang.RuntimeException: Unable to start activity ComponentInfo

java.lang.RuntimeException: Unable to start activity ComponentInfo 报错描述: 在导入别人项目运行时出现了这个报错: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.news/com.example.activity.DetailNews}: ja…

消息队列使用指南

介绍 消息队列是一种常用的应用程序间通信方法,可以用来在不同应用程序或组件之间传递数据或消息。消息队列就像一个缓冲区,接收来自发送方的消息,并存储在队列中,等待接收方从队列中取出并处理。 在分布式系统中,消…

PAD平板签约投屏-高端活动的选择

传统的现场纸质签约仪式除了缺乏仪式感之外还缺少互动性,如果要将签约的过程投放到大屏幕上更是需要额外的硬件设备成本。相比于传统的纸质签约仪式,平板现场电子签约的形式更加的新颖、更富有科技感、更具有仪式感。 平板签约投屏是应用于会议签字仪式的…

【面试经典150 | 二叉树】翻转二叉树

文章目录 写在前面Tag题目来源题目解读解题思路方法一:递归方法二:迭代 写在最后 写在前面 本专栏专注于分析与讲解【面试经典150】算法,两到三天更新一篇文章,欢迎催更…… 专栏内容以分析题目为主,并附带一些对于本题…

笔记69:Conv1d 和 Conv2d 之间的区别

笔记地址:D:\work_file\(4)DeepLearning_Learning\03_个人笔记\4. Transformer 网络变体 a a a a a a a a a a a

万户 ezOFFICE convertFile 文件读取漏洞复现

0x01 产品简介 万户OA ezoffice是万户网络协同办公产品多年来一直将主要精力致力于中高端市场的一款OA协同办公软件产品,统一的基础管理平台,实现用户数据统一管理、权限统一分配、身份统一认证。统一规划门户网站群和协同办公平台,将外网信息维护、客户服务、互动交流和日…

电脑搜不自己的手机热点,其余热点均可!

一、现象: 之前可正常连接,突然间发现收不到自己的WiFi信号,其余人均可收到。通过重复手机电脑关机、改变热点设置中的频段等方式均没解决,同事电脑和手机可搜索到我的WiFi。 二、问题: WiF驱动程序更新 三&#x…

【Docker】Docker Compose,yml 配置指令参考的详细讲解

作者简介: 辭七七,目前大二,正在学习C/C,Java,Python等 作者主页: 七七的个人主页 文章收录专栏: 七七的闲谈 欢迎大家点赞 👍 收藏 ⭐ 加关注哦!💖&#x1f…

delphi android打开外部文件,报错android.os.FileUriExposedException解决方法

Android 7.0强制启用了被称作 StrictMode的策略,带来的影响就是你的App对外无法暴露file://类型的URI了。 如果你使用Intent携带这样的URI去打开外部App(比如:打开系统相机拍照),那么会抛出FileUriExposedException异常。 Delphi 为Android…

用Java实现一对一聊天

目录 服务端 客户端 服务端 package 一对一用户; import java.awt.BorderLayout; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; imp…

C# 静态构造函数与类的初始化

静态构造函数: 基本概念: 静态构造函数用于初始化任何静态数据。 静态构造函数的常见特性: 静态构造函数不使用访问修饰符或不具有参数。因为静态构造函数由系统调用,无法人为调用,所以就不存在public、private等。…

【开源】基于Vue和SpringBoot的在线课程教学系统

项目编号: S 014 ,文末获取源码。 \color{red}{项目编号:S014,文末获取源码。} 项目编号:S014,文末获取源码。 目录 一、摘要1.1 系统介绍1.2 项目录屏 二、研究内容2.1 课程类型管理模块2.2 课程管理模块2…

Java IO流(五)(字符集基础知识简介)

字符集 计算机的存储规则(英文字符) 常见字符集介绍 a.GB2312字符集:1980年发布,1981年5月1日实施的简体中文汉字编码国家标准。收录7445个图形字符,其中包括6763个简体汉字 b.BIG5字符集:台湾地区繁体中…

【Angular开发】Angular在2023年之前不是很好

做一个简单介绍,年近48 ,有20多年IT工作经历,目前在一家500强做企业架构.因为工作需要,另外也因为兴趣涉猎比较广,为了自己学习建立了三个博客,分别是【全球IT瞭望】,【架构师酒馆】…

ES6中的继承,String类型方法的拓展

ES6中的继承: 主要是依赖extends关键字来实现继承,使用了extends实现继承不一定要constructor和super,因为没有的话会默认产生并调用它们。 在实现继承时,如果子类中有constructor函数,必须得在constructor中调用一下s…

目标检测、目标跟踪、重识别

文章目录 环境前言项目复现特征提取工程下载参考资料 环境 ubuntu 18.04 64位yolov5deepsortfastreid 前言 基于YOLOv5和DeepSort的目标跟踪 介绍过针对行人的检测与跟踪。本文介绍另一个项目,结合 FastReid 来实现行人的检测、跟踪和重识别。作者给出的2个主…

Sequential Modeling Enables Scalable Learning for Large Vision Models

目录 一、论文速读 1.1 摘要 1.2 论文概要总结 二、论文精度 2.1 论文试图解决什么问题? 2.2 论文中提到的解决方案之关键是什么? 2.3 论文提出的架构和损失函数是什么? 2.4 用于定量评估的数据集是什么?代码有没有开源&a…

基于c++版本的数据结构改-python栈和队列思维总结

##栈部分-(叠猫猫) ##抽象数据类型栈的定义:是一种遵循先入后出的逻辑的线性数据结构。 换种方式去理解这种数据结构如果我们在一摞盘子中取到下面的盘子,我们首先要把最上面的盘子依次拿走,才可以继续拿下面的盘子&…

Redis 命令全解析之 Hash类型

文章目录 ⛄介绍⛄命令⛄RedisTemplate API⛄应用场景 ⛄介绍 Hash类型,也叫散列,其value是一个无序字典,类似于Java中的 HashMap 结构。 String结构是将对象序列化为JSON字符串后存储,当需要修改对象某个字段时很不方便&#xf…