2

In Flask, I would like to stop video streaming, in order to perform another task (to redirect to main page). But instead, when streaming stops, the screen freezes to the last frame.

Here is my code:

def gen_temporary(cam):
    counter_frames=0    
    while counter_frames<100:
        frame = cam.get_frame()
        counter_frames+=1
        yield (b'--frame\r\n'
           b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
    yield redirect('/mainPage')


@app.route('/video_capture')
def video_capture():
    return Response(gen_temporary(VideoCamera()),
                mimetype='multipart/x-mixed-replace; boundary=frame')
SmartTom
  • 691
  • 7
  • 14
Mostafa
  • 1,501
  • 3
  • 21
  • 37

1 Answers1

1

You can't send a header such as an HTTP Location header after you've sent content. Additionally even if you could, if this response is a video stream it would probably be consumed by a <video> element or similar, and the redirect would only redirect the video request and the user would stay on the same page.

If you want to redirect the user elsewhere once a video is done playing, you'll have to do it via JavaScript by listening for the relevant event and then changing the URL.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • @Mostafa: That's a pretty quick reply. Did you even look at the documentation? https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events `ended` looks promising – Matti Virkkunen Jul 03 '16 at 15:22
  • it did not work, because i am streaming frame-by-frame not with video. I ended using http://stackoverflow.com/questions/8824141/how-to-redirect-from-one-url-to-another-url – Mostafa Jul 03 '16 at 16:36