pythonopencvgoogle-drive-apigoogle-colaboratory

How can I save a video produced with cv2.VideoWriter at Google-Colab to a Google Drive path?


I am trying to somehow copy the "video1.mkv" at a folder called "my folder".

There are better ways to copy but my tend is to read "video1.mkv" and create a new video by using the video that I've read before.

The problem is that I can't save it!

My codes are in Google-Colaboratory and I use Google-Drive as my directory for saving my files, codes or read anything from it. Everything is available in the "my folder", include the "name.ipnb" that I am coding in it.

So this is the code snippet:

import cv2 as cv

import os
from google.colab import drive
drive.mount('/content/drive')
os.chdir('/content/drive/My Drive/my folder')

caped = cv.VideoCapture('video1.mkv')

Start of problems:

x = cv.VideoWriter('./video2', cv.VideoWriter_fourcc(*'XVID'), 3, (600, 500))

while(caped.isOpened()):

  ret, frame = caped.read()

  if ret == True:
    x.write(frame)

  else:
    break
  

caped.release()
x.release()

There is not any error raised but "video2" won't save.


Solution

  • As you mentioned there are better ways to copy

    !cp -r "/content/video" "/content/drive/MyDrive"
    

    But for this specific problem the file name is missing the extension

    caped = cv.VideoCapture('video1.mkv')
    
    x = cv.VideoWriter('video2.avi', cv.VideoWriter_fourcc(*'XVID'), 3, (600, 500))
    
    while caped.isOpened():
        ret, frame = caped.read()
        if ret:
            x.write(frame)
        else:
            break
    
    caped.release()
    x.release()