python视频分段_Python玩转视频处理(四):视频按场景进行分割
在上一篇文章(按場景進行分割視頻的工具——PySceneDetect。
下面開始介紹下PySceneDetect及其安裝使用方法。
1. 什么是PySceneDetect
PySceneDetect是一個命令行工具和Python庫,用于分析視頻,查找場景更改或剪輯。
PySceneDetect集成了外部工具(例如mkvmerge , ffmpeg ),可在使用split-video命令時自動將視頻分割為單個片段。還可以為視頻生成逐幀分析,稱為統(tǒng)計文件,以幫助確定最佳閾值或檢測特定視頻的模式/其他分析方法。
PySceneDetect使用兩種主要的檢測方法: detect-threshold (將每個幀與設置的黑電平進行比較,對于檢測從黑色到黑色的淡入和淡出有用)和detect-content (比較每個幀,依次查找內容的變化,有用)用于檢測視頻場景之間的快速切換,盡管處理速度較慢)。每種模式的參數(shù)略有不同,并在文檔中進行了詳細說明.。
通常,如果要使用淡入/淡出/切成黑色來檢測場景邊界,請使用檢測閾值模式。如果視頻在內容之間使用大量快速剪切,并且沒有明確定義的場景邊界,則應使用" 檢測內容"模式。一旦知道要使用哪種檢測模式,就可以嘗試以下建議的參數(shù),或生成統(tǒng)計文件(使用-s / –stats參數(shù)),以確定正確的參數(shù)-具體來說,是正確的閾值.
2. PySceneDetect的安裝
PySceneDetect依賴于Python模塊numpy,OpenCV(cv2模塊)和tqdm(進度條模塊,用來顯示處理進度),安裝命令如下:
$ pip install scenedetect
PySceneDetect基于ffmpeg和mkvmerge對視頻進行裁剪。ffmpeg 是一個開源軟件,可以運行音頻和視頻多種格式的錄影、轉換、流功能,它功能強大,用途廣泛,是視頻處理最常用的開源軟件。
mkvmerge是MKV工具集MKVToolNix中的一個軟件,可以將多媒體文件封裝、合并、混流為 MKV 文件。
安裝完成后可以通過命令行或代碼兩種方式進行使用。
3. 命令行使用
PySceneDetect在命令行中使用scenedetect命令進行操作,命令格式如下:
$ scenedetect --input my_video.mp4 --output my_video_scenes --stats my_video.stats.csv detect-content list-scenes save-images
參數(shù)說明:
常用的參數(shù)說明如下:–input :輸入視頻文件的路徑
–output :指定輸出目錄(可選)
–stats:生成統(tǒng)計文件(可選)
time:用于設置輸入視頻持續(xù)時間/長度或開始/結束時間。
detect-content:切分視頻基于內容檢測算法。
detect-threshold:切分視頻基于閾值檢測算法。
list-scenes:打印場景列表并輸出到CSV文件。
save-images:為每個場景保存視頻中的圖像。
split-video:使用ffmpeg或mkvMerge對視頻進行分割。
完整的參數(shù)列表可使用scenedetect help all命令進行查看。
示例:
$ scenedetect --input demo.mp4 detect-content list-scenes save-images split-video
運行完成后會在當前文件夾生成視頻片段,片段截圖以及csv文件,如下:
demo-Scene-001-01.jpg demo-Scene-004-03.jpg ...
demo-Scene-001-02.jpg demo-Scene-004.mp4
...
demo-Scenes.csv
csv文件中包含片段的幀、時間、長度等信息,內容如下:
Timecode List: 00:07.9 00:14.6 00:38.7 00:45.3 00:48.9 01:00.0 01:12.3 01:21.5 01:36.3
Scene Number Start Frame Start Timecode Start Time (seconds) End Frame End Timecode End Time (seconds) Length (frames) Length (timecode) Length (seconds)
1 0 00:00.0 0 190 00:07.9 7.917 190 00:07.9 7.917
2 190 00:07.9 7.917 350 00:14.6 14.583 160 00:06.7 6.667
3 350 00:14.6 14.583 928 00:38.7 38.667 578 00:24.1 24.083
...
4. 在Python中使用
在Python中使用PySceneDetect主要用到下面幾個類:VideoManager:用于加載視頻并提供搜索;
SceneManager:用于協(xié)調SceneDetector,VideoManager和可選的StatsManager對象的高級管理器;
FrameTimecode:用于存儲時間碼以及對時間碼值進行算術運算(加/減/比較),并具有幀級的精確度;
StatsManager:用于存儲/緩存幀指標,以加快在同一視頻上后續(xù)場景檢測的運行速度,并可以保存到CSV文件或從CSV中加載緩存;
SceneDetector:用于實現(xiàn)檢測算法的基類,如ContentDetector,ThresholdDetector等。
官方的示例代碼如下:
from __future__ import print_function
import os
import scenedetect
from scenedetect.video_manager import VideoManager
from scenedetect.scene_manager import SceneManager
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors import ContentDetector
STATS_FILE_PATH = 'testvideo.stats.csv'
def main():
# Create a video_manager point to video file testvideo.mp4. Note that multiple
# videos can be appended by simply specifying more file paths in the list
# passed to the VideoManager constructor. Note that appending multiple videos
# requires that they all have the same frame size, and optionally, framerate.
video_manager = VideoManager(['testvideo.mp4'])
stats_manager = StatsManager()
scene_manager = SceneManager(stats_manager)
# Add ContentDetector algorithm (constructor takes detector options like threshold).
scene_manager.add_detector(ContentDetector())
base_timecode = video_manager.get_base_timecode()
try:
# If stats file exists, load it.
if os.path.exists(STATS_FILE_PATH):
# Read stats from CSV file opened in read mode:
with open(STATS_FILE_PATH, 'r') as stats_file:
stats_manager.load_from_csv(stats_file, base_timecode)
start_time = base_timecode + 20 # 00:00:00.667
end_time = base_timecode + 20.0 # 00:00:20.000
# Set video_manager duration to read frames from 00:00:00 to 00:00:20.
video_manager.set_duration(start_time=start_time, end_time=end_time)
# Set downscale factor to improve processing speed.
video_manager.set_downscale_factor()
# Start video_manager.
video_manager.start()
# Perform scene detection on video_manager.
scene_manager.detect_scenes(frame_source=video_manager)
# Obtain list of detected scenes.
scene_list = scene_manager.get_scene_list(base_timecode)
# Like FrameTimecodes, each scene in the scene_list can be sorted if the
# list of scenes becomes unsorted.
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(),))
# We only write to the stats file if a save is required:
if stats_manager.is_save_required():
with open(STATS_FILE_PATH, 'w') as stats_file:
stats_manager.save_to_csv(stats_file, base_timecode)
finally:
video_manager.release()
if __name__ == "__main__":
main()Python Interface & Integration With Other Applications?pyscenedetect.readthedocs.io
總結
以上是生活随笔為你收集整理的python视频分段_Python玩转视频处理(四):视频按场景进行分割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Odoo产品分析 (三) -- 人力资源
- 下一篇: ABAP中的subroutine和fun