我有一个HLS流,并希望使用python中的opencv提取帧以用于计算机视觉。我尝试探索ffmpeg,但它似乎很容易读取.mp4,但不适用于hls流(m3u8)。ffmpeg-python是否有其他任何选项或其他API从HLS流中提取帧。
这是我想尝试的示例代码,但不适用于VIDEO_URL
from imutils.video import VideoStream
import ffmpeg
import cv2
import numpy as np
import subprocess as sp
VIDEO_URL = "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
VIDEO_FILE = "sampleStream.mp4"
process = (
ffmpeg
.input(VIDEO_FILE)
#.input(VIDEO_URL)
.output('pipe:', format='rawvideo', pix_fmt='rgb24')
.run_async(pipe_stdout=True)
)
tar = 200
val = 0
while True:
val = val + 1
in_bytes = process1.stdout.read(100 * 200 * 3)
if not in_bytes:
print('Breaking - No bytes found.')
break
in_frame = (
np
.frombuffer(in_bytes, np.uint8)
.reshape([100, 200, 3])
)
if val == tar:
print('Writing image...')
cv2.imwrite("sample.jpg", in_frame)
break
您可以用来cv2.VideoCapture()
直接读取HLS流。为什么要受苦?
import cv2
import sys
VIDEO_URL = "http://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
cap = cv2.VideoCapture(VIDEO_URL)
if (cap.isOpened() == False):
print('!!! Unable to open URL')
sys.exit(-1)
# retrieve FPS and calculate how long to wait between each frame to be display
fps = cap.get(cv2.CAP_PROP_FPS)
wait_ms = int(1000/fps)
print('FPS:', fps)
while(True):
# read one frame
ret, frame = cap.read()
# TODO: perform frame processing here
# display frame
cv2.imshow('frame',frame)
if cv2.waitKey(wait_ms) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
输出:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句