-2

I think it is not a duplicate of previous problem.

The background is i am doing web development. I used opencv to gnerate video and put the video on website.

Before i try it can it worked. But recently the video suddenly cannot be display in browser

Opencv code


video = cv2.VideoCapture("./this.mp4")     
ret, frame = video.read()
fps, w, h = 30, frame.shape[1], frame.shape[0]                     
result = cv2.VideoWriter('this.mp4',cv2.VideoWriter_fourcc(*'mp4v'),fps, (w,h))
        
while(True):
    ret, frame = video.read()
    if ret == True:
        fps, w, h = 30, frame.shape[1], frame.shape[0]                     
        result = cv2.VideoWriter('this.mp4',cv2.VideoWriter_fourcc(*'mp4v'),fps, (w,h))
 
        result.write(frame)
        if cv2.waitKey(1) & 0xFF == ord('s'):
            break

    # Break the loop
    else:
        break



print("The video was successfully saved")

Html code:

<video controls>
  <source src="this.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

It is not the path problem i have checked it. I place the orginal file and the generated file in the smae folder and display them, only the generated video fails to be displayed.

Does it the html standard change recently? Or the encoding problem. but i think both of them are not true since i have tested it sucessully 1 month ago

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Fish Ung
  • 17
  • 8
  • this is indeed a duplicate. https://stackoverflow.com/questions/76422796/video-created-with-opencv-python-cannot-be-uploaded-to-social-media-platforms -- and many more https://stackoverflow.com/search?tab=newest&q=opencv%20mp4v%20&searchOn=3 – Christoph Rackwitz Jun 23 '23 at 09:26
  • further, stop using OpenCV. it's for computer vision, not for serving video files. your code merely transcodes an existing video file, without any further processing, which you could have served as is. additionally, you are reading from the same file you're trying to write into (same file name anyway, ignoring behavior of file systems on windows vs. elsewhere). that's a terrible idea because it's an error. on top of *that*, you are opening the same file TWICE for writing. writing a video file doesn't append. it deletes the file, then starts fresh, from only that which you write. – Christoph Rackwitz Jun 23 '23 at 09:28

1 Answers1

1

One option is to rencode the mp4 file you got using ffmpeg

enter image description here

I'd recommend using webm container compactable with html. Change below line and let me know if it's works.

result = cv2.VideoWriter('file.webm',cv2.VideoWriter_fourcc(*'vp80'),fps, (w,h))

in html

<video controls>
  <source src="file.webm" type="video/webm">
  Your browser does not support the video tag.
</video>
  • chrome supports H.264 through system media APIs, instead of bringing its own codec. those comparison tables commonly don't mention that because they're trying to get rid of that family of codecs in favor of others. – Christoph Rackwitz Jun 23 '23 at 09:23