Warm tip: This article is reproduced from stackoverflow.com, please click
computer-vision ffmpeg opencv python

Extracting frames from HLS stream in python

发布于 2020-04-08 09:22:29

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

Questioner
john0609
Viewed
150
karlphillip 2020-02-01 20:35

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: