本文整理匯總了Python中cv2.CAP_PROP_FPS屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.CAP_PROP_FPS屬性的具體用法?Python cv2.CAP_PROP_FPS怎麽用?Python cv2.CAP_PROP_FPS使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.CAP_PROP_FPS屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: initialize
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def initialize(self):
self.capture = cv2.VideoCapture(self.camera)
if not self.capture.isOpened():
print("Error opening video camera")
return
# cap.set(cv2.CAP_PROP_FPS, 5)
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
if self.record:
self.videowriter = cv2.VideoWriter(self.filename,
cv2.VideoWriter_fourcc('m', 'p', '4', 'v'),
10,
(self.width, self.height),
isColor=True)
示例2: extract_allframescommand
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def extract_allframescommand(filename):
if filename:
pathDir = str(filename[:-4])
if not os.path.exists(pathDir):
os.makedirs(pathDir)
picFname = '%d.png'
saveDirFilenames = os.path.join(pathDir, picFname)
print(saveDirFilenames)
fname = str(filename)
cap = cv2.VideoCapture(fname)
fps = cap.get(cv2.CAP_PROP_FPS)
amount_of_frames = cap.get(7)
print('The number of frames in this video = ',amount_of_frames)
print('Extracting frames... (Might take awhile)')
command = str('ffmpeg -i ' +'"'+ str(fname)+'"' + ' ' + '-q:v 1' + ' ' + '-start_number 0' + ' '+'"'+ str(saveDirFilenames)+'"')
print(command)
subprocess.call(command, shell=True)
print('All frames are extracted!')
else:
print('Please select a video to convert')
示例3: test_input_framerate
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def test_input_framerate(c_ffmpeg):
"""
Testing "-input_framerate" parameter provided by WriteGear(in Compression Mode)
"""
stream = cv2.VideoCapture(return_testvideo_path()) # Open stream
test_video_framerate = stream.get(cv2.CAP_PROP_FPS)
output_params = {"-input_framerate": test_video_framerate}
writer = WriteGear(
output_filename="Output_tif.mp4",
custom_ffmpeg=c_ffmpeg,
logging=True,
**output_params
) # Define writer
while True:
(grabbed, frame) = stream.read()
if not grabbed:
break
writer.write(frame)
stream.release()
writer.close()
output_video_framerate = getFrameRate(os.path.abspath("Output_tif.mp4"))
assert test_video_framerate == output_video_framerate
os.remove(os.path.abspath("Output_tif.mp4"))
示例4: __init__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def __init__(self, device=None, size=None, fps=None, sync=False):
self.device = device or 0
self.size = size or (480, 640)
fps = fps or 30
self.cap = cv2.VideoCapture(self.device)
cap_height, cap_width = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT), self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
if cap_height != self.size[0]:
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.size[0])
if cap_width != self.size[1]:
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.size[1])
cap_fps = self.cap.get(cv2.CAP_PROP_FPS)
if cap_fps != fps:
self.cap.set(cv2.CAP_PROP_FPS, fps)
if sync:
raise ValueError("sync not supported")
示例5: main
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def main():
args = set_args()
dest = args.destination_path + '.json'
if os.path.isfile(dest) and not args.force:
if input("'%s' already exists. Do you want to override it? [y/n]: " % args.destination_path) != 'y':
print('exiting')
exit(4)
file = open(dest, 'w')
cap = extract_video.get_capture(args.capture_path)
if cap is None or cap.get(cv2.CAP_PROP_FPS) == 0:
if extract_video.youtube_url_validation(args.capture_path):
print("Cannot access video in URL. Please check the URL is a valid YouTube video")
exit(2)
print("Cannot access video in file. Please make sure the path to the file is valid")
exit(3)
get_data(cap, file, to_float(args.launch_time), args.out, args.destination_path, args.live, args.from_launch)
示例6: main
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def main():
args = set_args()
dest = args.destination_path + '.json'
if os.path.isfile(dest) and not args.force:
if input("'%s' already exists. Do you want to override it? [y/n]: " % args.destination_path) != 'y':
print('exiting')
exit(4)
file = open(dest, 'w')
cap = extract_video.get_capture(args.capture_path)
if cap is None or cap.get(cv2.CAP_PROP_FPS) == 0:
if extract_video.youtube_url_validation(args.capture_path):
print("Cannot access video in URL. Please check the URL is a valid YouTube video")
exit(2)
print("Cannot access video in file. Please make sure the path to the file is valid")
exit(3)
get_data(cap, file, to_float(args.launch_time), args.out, args.destination_path, args.live)
示例7: skip_from_launch
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def skip_from_launch(self, cap, key, time, thresh=None):
"""
Move the capture to T+time (time can be negative) and returns the frame index.
:param cap: OpenCV capture
:param time: delta time from launch to skip to
:return: index of requested frame
"""
if thresh is None:
thresh = self.extractor.image_dict[key][2]
number_of_frames = int(cap.get(cv2.CAP_PROP_FPS) * time) + self.search_switch(cap, key, thresh)
number_of_frames = max(number_of_frames, 0)
number_of_frames = min(number_of_frames, cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.set(cv2.CAP_PROP_POS_FRAMES, number_of_frames)
return number_of_frames
示例8: __init__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def __init__(self, video_source, video_width, video_height, video_fps, queue_size=1):
self.video_fps = video_fps
vc = cv2.VideoCapture(video_source)
if hasattr(cv2, 'cv'):
vc.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, video_width)
vc.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, video_height)
vc.set(cv2.cv.CV_CAP_PROP_FPS, video_fps)
else:
vc.set(cv2.CAP_PROP_FRAME_WIDTH, video_width)
vc.set(cv2.CAP_PROP_FRAME_HEIGHT, video_height)
vc.set(cv2.CAP_PROP_FPS, video_fps)
self.stream = vc
self.stopped = False
self.queue = Queue(maxsize=queue_size)
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
示例9: getInfo
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def getInfo(sourcePath):
cap = cv2.VideoCapture(sourcePath)
info = {
"framecount": cap.get(cv2.CAP_PROP_FRAME_COUNT),
"fps": cap.get(cv2.CAP_PROP_FPS),
"width": int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
"height": int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
"codec": int(cap.get(cv2.CAP_PROP_FOURCC))
}
cap.release()
return info
#
# Extracts one frame for every second second of video.
# Effectively compresses a video down into much less data.
#
示例10: VideoToSequence
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def VideoToSequence(path, time):
video = cv2.VideoCapture(path)
dir_path = 'frames_tmp'
os.system("rm -rf %s" % dir_path)
os.mkdir(dir_path)
fps = int(video.get(cv2.CAP_PROP_FPS))
length = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
print('making ' + str(length) + ' frame sequence in ' + dir_path)
i = -1
while (True):
(grabbed, frame) = video.read()
if not grabbed:
break
i = i + 1
index = IndexHelper(i*time, len(str(time*length)))
cv2.imwrite(dir_path + '/' + index + '.png', frame)
# print(index)
return [dir_path, length, fps]
示例11: load_video
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def load_video(video, all_frames=False):
cap = cv2.VideoCapture(video)
fps = cap.get(cv2.CAP_PROP_FPS)
if fps > 144 or fps is None:
fps = 25
frames = []
count = 0
while cap.isOpened():
ret, frame = cap.read()
if isinstance(frame, np.ndarray):
if int(count % round(fps)) == 0 or all_frames:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frames.append(center_crop(resize_frame(frame, 256), 256))
else:
break
count += 1
cap.release()
return np.array(frames)
示例12: __init__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def __init__(self, sources='streams.txt', img_size=416, half=False):
self.mode = 'images'
self.img_size = img_size
self.half = half # half precision fp16 images
if os.path.isfile(sources):
with open(sources, 'r') as f:
sources = [x.strip() for x in f.read().splitlines() if len(x.strip())]
else:
sources = [sources]
n = len(sources)
self.imgs = [None] * n
self.sources = sources
for i, s in enumerate(sources):
# Start the thread to read frames from the video stream
print('%g/%g: %s... ' % (i + 1, n, s), end='')
cap = cv2.VideoCapture(0 if s == '0' else s)
assert cap.isOpened(), 'Failed to open %s' % s
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) % 100
_, self.imgs[i] = cap.read() # guarantee first frame
thread = Thread(target=self.update, args=([i, cap]), daemon=True)
print(' success (%gx%g at %.2f FPS).' % (w, h, fps))
thread.start()
print('') # newline
示例13: compound_bgm
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def compound_bgm(video_path, bgm_path):
"""
通過視頻、BGM 合成一段視頻
:param video_path: 視頻路徑
:param bgm_path: BGM路徑
:return:
"""
# $ ffmpeg -i 2_003_014.mp4 -vn -y -acodec copy 3.aac
# 1.提前temp.mp4這個視頻的BGM,文件結果為:temp.aac
# os.system('ffmpeg -i temp.mp4 -vn -y -acodec copy temp.aac')
# 2.獲取視頻的長度
cap = cv2.VideoCapture(video_path)
# 幀率
fps = cap.get(cv2.CAP_PROP_FPS)
# 總幀數
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
# 總時長-秒,這裏做取整操作 【浮點類型】
time_count = math.floor(frame_count / fps)
print('幀率:%f,總幀數:%d' % (fps, frame_count))
print(time_count)
# 3.截取音頻
# 為了簡單,這裏一般不會超過一分鍾
bgm_temp_path = get_temp_path(bgm_path, 'temp_new')
os.system('ffmpeg -i %s -ss 00:00:00 -t 00:00:%d -acodec copy %s' % (bgm_path, time_count, bgm_temp_path))
# 3.1 刪除源音頻並重命令當前文件
os.remove(bgm_path)
os.rename(bgm_temp_path, bgm_path)
# 4.視頻、音頻合二為一
video_temp_path = get_temp_path(video_path, 'temp')
os.system('ffmpeg -i %s -i %s -vcodec copy -acodec copy %s' % (video_path, bgm_path, video_temp_path))
os.remove(video_path)
os.rename(video_temp_path, video_path)
print('音視頻合成完成~')
示例14: add_water_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def add_water_mask(video_path, mask_word):
"""
給視頻增加水印
:param video_part3: 視頻源
:param mask_word: 水印文字
:return:
"""
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
# 保證幀率不變
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video_temp_path = get_temp_path(video_path, 'temp')
video_writer = cv2.VideoWriter(video_temp_path, fourcc, fps, img_size)
ret, frame = cap.read()
while ret:
# 文字在圖中的坐標(注意:這裏的坐標原點是圖片左上角)
x, y = img_size[0] - 200, img_size[1] - 50
cv2.putText(img=frame, text=mask_word,
org=(x, y), fontFace=cv2.FONT_HERSHEY_COMPLEX_SMALL,
fontScale=1, color=(255, 255, 255))
video_writer.write(frame)
ret, frame = cap.read()
# 刪除源文件,並重命名臨時文件
os.remove(video_path)
os.rename(video_temp_path, video_path)
print('水印添加完成~')
video_writer.release()
cap.release()
示例15: generate_dataset
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import CAP_PROP_FPS [as 別名]
def generate_dataset(videos_path, framerate, width, height):
"""Converts videos from specified path to ndarrays of shape [numberOfVideos, -1, width, height, 1]
Args:
videos_path: Inside the 'videos/' directory, the name of the subdirectory for videos.
framerate: The desired framerate of the dataset.
width: The width we will resize the videos to.
height: The height we will resize the videos to.
Returns:
The dataset with the new size and framerate, and converted to monochromatic.
"""
dataset = []
video_index = 0
for playlist in os.listdir('videos/' + videos_path):
for video_name in os.listdir('videos/{}/{}'.format(videos_path, playlist)):
dataset.append([])
print('Video: {}'.format(video_name))
video = cv2.VideoCapture('videos/{}/{}/{}'.format(videos_path, playlist, video_name))
while video.isOpened():
success, frame = video.read()
if success:
frame = preprocess_image(frame, width, height)
dataset[video_index].append(frame)
frame_index = video.get(cv2.CAP_PROP_POS_FRAMES)
video_framerate = video.get(cv2.CAP_PROP_FPS)
video.set(cv2.CAP_PROP_POS_FRAMES, frame_index + video_framerate // framerate)
last_frame_index = video.get(cv2.CAP_PROP_FRAME_COUNT)
if frame_index >= last_frame_index:
# Video is over
break
else:
break
dataset[video_index] = np.reshape(dataset[video_index], (-1, width, height, 1))
video_index += 1
return dataset