Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoWriter class does not support stream copy #14573

Open
DerekGloudemans opened this issue May 16, 2019 · 5 comments
Open

VideoWriter class does not support stream copy #14573

DerekGloudemans opened this issue May 16, 2019 · 5 comments

Comments

@DerekGloudemans
Copy link

DerekGloudemans commented May 16, 2019

System information (version)
  • OpenCV => 3.1 with bindings in Python 3.6
  • Operating System / Platform =>Ubuntu 18.04 LTS
  • Compiler => Visual Studio 2015
    -->
Detailed description

The VideoWriter object does not have working stream copy functionality. Using a command line call to ffmpeg, for instance, one can read a video stream as input (akin to the VideoCapture object) and directly copy this stream into an output file without any decoding or re-encoding, which saves a lot of computation time. In OpenCV, if no codec is specified for the VideoWriter object, no file is output. Documentation suggests that entering a value of 0 into the fourcc codec code and a framerate of 0 when constructing the VideoWriter object should accomplish this but does not.

https://docs.opencv.org/3.4/dd/d9e/classcv_1_1VideoWriter.html
"If FFMPEG is enabled, using codec=0; fps=0; you can create an uncompressed (raw) video file."

The following code produces no output file.
```.py

    fourcc = 0

    fps = 0

    size =  1080,720

    out = VideoWriter("outfile.avi",outvid, fourcc, float(fps), size)    
```

-->

@mshabunin
Copy link
Contributor

create an uncompressed (raw) video file

is not the same as

directly copy this stream into an output file without any decoding or re-encoding

VideoCapture and VideoWriter do not provide interface to access raw compressed video stream, except maybe MJPEG in some cases.

Make sure you actually use FFmpeg backend by setting apiPreference parameter: VideoWriter("outfile.avi", cv2.CAP_FFMPEG, ...)

@DerekGloudemans
Copy link
Author

My apologies, I mistakenly mixed two issues in my comment. The second is a valid issue though; there is no support for the copy codec of ffmpeg in the VideoWriter class even using the ffmpeg backend.

@monocongo
Copy link

I have also had this issue. The solution proposed in the VideoWriter documentation (codec=0; fps=0;) does not produce a file containing raw (uncompressed/unencoded) data as expected, and in fact, it produces no file at all.

I can get the result I'm after (raw H.265 data as an MP4 file) using ffmpeg like so:

$ ffmpeg -i rtsp://@192.168.123.45:554 -acodec copy -vcodec copy /data/abc.mp4

If possible I'd like to be able to do the same thing using OpenCV, although I assume that OpenCV will essentially just be providing a wrapper interface for calling an ffmpeg command such as the above. Maybe it's just as simple to utilize ffmpeg-python for this, like so:

import ffmpeg
stream = ffmpeg.input("rtsp://@192.168.123.45:554")
stream = ffmpeg.output(stream, "/data/abc.mp4",
                       **{"codec:v": "copy",
                          "rtsp_transport": "tcp",
                          "y": None
                          }
                       )
ffmpeg.run(stream)

@mshabunin
Copy link
Contributor

I think that media stream repackaging is out of scope of OpenCV and direct usage of FFmpeg and GStreamer is the right way to do it.

If you have evidence of issue with documentation and/or videoio module, please add more information here: platform, build configuration, minimal reproducer app.

@DerekGloudemans
Copy link
Author

DerekGloudemans commented May 30, 2019

The following application produces no output file yet should according to documentation as indicated above.
Ubuntu 18.04 LTS
Python 3.6.8
OpenCV 4.1.0

# Create a VideoCapture object from IP stream
cap = cv2.VideoCapture('rtsp://username:password@192.168.1.0/axis-media/media.amp',cv2.CAP_FFMPEG)

# Check if camera opened successfully
if (cap.isOpened() == False): 
   print("Unable to read camera feed")

# Define the codec and create VideoWriter object.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

################ Line that does not behave as expected #################
out = cv2.VideoWriter('out_file.avi',cv2.CAP_FFMPEG,0,0,(frame_width,frame_height))

while(True):
   ret, frame = cap.read()

   if ret == True: 

      # Write the frame into output file
      out.write(frame)
      # Display the resulting frame    
      cv2.imshow('frame',frame)
    
      # Press Q on keyboard to stop recording
      if cv2.waitKey(1) & 0xFF == ord('q'):
          break

   # Break the loop if no frame returned
   else:
      break 

# release video write and read objects
out.release()  
cap.release()
cv2.destroyAllWindows() 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants