程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using Python through the subtitle file SRT to intercept video clips

編輯:Python

First , Want to use the code in this article , You must have a subtitle file .srt, And change the file suffix to .txt

.srt The file format is as follows :

The code is as follows :

"""
This is a video detection for a single keyword
"""
import cv2 as cv
import numpy as np
import re
video_file = cv.VideoCapture(r"E:\900 Second, Chang'e sets the moon .mp4")
fps = video_file.get(5)
print(" The frame of this video is {}".format(fps))
#==================================================================================#
"""
Experimental stage : This content is used to get the key fields you want to find , Time position in video , And intercept the video
The final stage : Text matching , Who has the highest similarity , Just choose which paragraph to intercept ,
And a lot of knowledge is involved in this task : Text similarity matching 、 Synonym training 、 How to integrate txt File subtitles and time division and then restore and so on
"""
#name=input(" Please enter the extracted content :")
name=input(" Please enter the key fields or contents you need to find :")
with open("CHS_ The Goddess Chang's fly to the moon .txt","r",encoding="utf-8") as f:
flie_data=f.readlines()
for i,readline in enumerate(flie_data):
#print(readline)
# matchObj=re.match(name,readline)# When the match , If the first character does not match , There will be a matching failure
matchObj = re.search(name, readline)
print(matchObj)
if matchObj is not None:
print(" The position of the matching character {}".format(i))
print(" The time period is {}".format(flie_data[i-1]))
break
# list_time=[]
# list_frames_time=[]
# import re
# name_all=[" fuel "," Failure "]
# with open("CHS_ The Goddess Chang's fly to the moon .txt","r",encoding="utf-8") as f:
# flie_data=f.readlines()
# for name in name_all:
# for i,readline in enumerate(flie_data):
# #print(readline)
# # matchObj=re.match(name,readline)# When the match , If the first character does not match , There will be a matching failure
# matchObj = re.search(name, readline)
# print(matchObj)
# if matchObj is not None:
# list_time.append(flie_data[i-1])
# print(" The position of the matching character {}".format(i))
# print(" The time period is {}".format(flie_data[i-1]))
# list_time.sort()# Sort the time ( From small to large )
# print(" The video time to be intercepted ",list_time)
# =============================================================================#
# First, do manual input , And convert the frame and time
# start_frame = input(" Please enter the number of start frames ,( The input format is : branch : second , Milliseconds or frames ):")
start_frame=flie_data[i-1]
if "-->" in start_frame:
start_frame_1 = start_frame.split(" --> ")
start_frame = start_frame_1[0]
end_frame = start_frame_1[1]
print(" Starting time --> End time ")
else:
end_frame = input(" Please end the start frame number ,( The input format is : branch : second , Milliseconds or frames ):")
"""
If you get the time format, convert it to frame format
When yes is entered : branch : second . millisecond
1 when =60 branch =3600 second =3600000 millisecond
If you get a frame format , That just doesn't have to change
"""
# start_frame_spilt=start_frame.split(":")
# ==============================================================================#
if ":" in start_frame:
print(" The input is the time system !")
name_str = start_frame.split(":")
start_frame = (int(name_str[0]) * 3600 + int(name_str[1]) * 60 + float(name_str[2].replace(",", "."))) * fps
#list_frames_time.append(start_frame)
else:
print(" The value entered is decimal ( frame )!")
start_frame = int(start_frame)
if ":" in end_frame:
print(" The input is the time system !")
name_str = end_frame.split(":")
end_frame = (int(name_str[0]) * 3600 + int(name_str[1]) * 60 + float(name_str[2].replace(",", "."))) * fps
#list_frames_time.append(end_frame)
else:
print(" The value entered is decimal ( frame )!")
end_frame = int(end_frame)
# ==============================================================================================#
"""
If the time before and after is relatively small , That is, the number of frames is less than 1, Then you can't cut the picture , So cut directly before and after 5 Frame to fill
"""
if (end_frame - start_frame) < 1:
start_frame = start_frame - 5
end_frame = end_frame + 5
else:
pass
#=============================================================================================#
fourcc = cv.VideoWriter_fourcc(*'MJPG')
count_frame = 0
if video_file.isOpened():
print("video can open!")
success, frames = video_file.read()
out = cv.VideoWriter('output.avi', fourcc, 25, (frames.shape[1], frames.shape[0])) # Note that the length and width must not be wrong , Otherwise, there will be no result
while success:
success, frames = video_file.read()
count_frame += 1
if count_frame >= start_frame and count_frame <= end_frame:
print(" Cut off the {} frame ".format(count_frame))
out.write(frames)
if count_frame > end_frame:
break
else:
print("video open failure!")
video_file.release()

Be careful , This is a simple use python The regular matching function of , You can change the code according to your own functions , Later, we will send another article about the matching of multiple keywords to intercept the content of video clips . If you have any good ideas , We can discuss together , Progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved