I have a HLS stream and wanted to extract frame as it appears for computer vision using opencv in python. I have tried exploring ffmpeg but it seems to read .mp4 easily but not the hls stream (m3u8). Is there any other option or other API of ffmpeg-python to extract frame from HLS stream.
Here is the sample code which I thought of giving a try but is not working with 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
You can use cv2.VideoCapture()
to read the HLS stream directly. Why suffer?
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()
Output:
I tried this approach as well which was giving me the following error: tls @ 0x7f93a9f1ff40] Error decoding the received TLS packet. [hls @ 0x7f93a500ec00] Error when loading first segment 'bitdash-a.akamaihd.net/content/sintel/hls/video/6000kbit/…' OpenCV: Couldn't read video stream from file "bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8" !!! Unable to open URL
Update to a more recent OpenCV! I'm using 4.2.0 on Windows.
I am using 4.2.0 but on mac. Seems like permission issue.
Glad it helped you! Feel free to up vote the answers that helped you solve the problem and click on the checkbox near an answer to select it as the official problem solver. By doing these things you will be helping future visitors find their answers more quickly.