Issue
This Content is from Stack Overflow. Question asked by Muhamed Essam
I am trying to visualize a live stream video and at the same time i would like to preview last few minutes from it, trim it or even preview multiple times.
The Problem is although I can see the post request is triggered but still it doesn’t work
########## Generator function #######
def gen(vid_source, cam_idx, preview=False):
'''
Video streaming generator function
vid_source: source to capture from
cam_idx: index of the displayed camera
'''
global match_folder, recording_folder, events_folder
if preview==False:
recording_folder = './records'
live_stream_record_path = f'{recording_folder}/cam_{cam_idx}.mp4' #where the live stream will be recorded
cap = cv2.VideoCapture(vid_source)
fps = cap.get(cv2.CAP_PROP_FPS)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
if preview==False:
live_stream_vid_writer = cv2.VideoWriter(live_stream_record_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (int(width), int(height)))
frame_num = 0
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, img = cap.read()
if ret == True:
if preview==False:
live_stream_vid_writer.write(img)
frame_num +=1
if os.path.exists(f'./records/preview{cam_idx}_imgs')==False:
os.mkdir(f'./records/preview{cam_idx}_imgs')
cv2.imwrite(f'./records/preview{cam_idx}_imgs/{frame_num}.jpg',img)
img = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
frame = cv2.imencode('.jpg', img)[1].tobytes()
yield (b'--framern'b'Content-Type: image/jpegrnrn' + frame + b'rn')
time.sleep((1/fps))
else:
break
############# routes ##############
@app.route('/cam1_video_feed')
async def cam1_video_feed():
return Response(gen('1.mp4',0),
mimetype='multipart/x-mixed-replace; boundary=frame')
############## html #################
<img src="{{ url_for('cam1_video_feed') }}" width='200px' height='200px'>```
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.