Skip to content

Instantly share code, notes, and snippets.

@laygond
Last active August 14, 2019 00:56
Show Gist options
  • Save laygond/b20b24613e640417d91fae05c90ae9a8 to your computer and use it in GitHub Desktop.
Save laygond/b20b24613e640417d91fae05c90ae9a8 to your computer and use it in GitHub Desktop.
Write Video OpenCV
video_name = "sample.mp4"
input_video_path = os.path.join("Input_Video", video_name) # USER must Edit
# Load video
cap = cv2.VideoCapture(input_video_path)
# Get General Info
fps = cap.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = int(frame_count/fps)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # float
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # float
# Create folder to place output videos at same level directory as input folder
try:
os.mkdir("Output_Video")
except FileExistsError:
print ("Output Folder is ready already")
# Write video
video_name_part = video_name.split(".") # sample & mp4 has been split
dest_path = os.path.join("Output_Video", video_name_part[0] + "_output." + video_name_part[1])
fourcc = cv2.VideoWriter_fourcc(*'X264') #*'MP4V' or you may have to try a different combination check https://www.fourcc.org/
out = cv2.VideoWriter(dest_path, fourcc, fps, (width,height))
for frame_num in range(frame_count):
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
_,frame=cap.read()
#Apply some processing and then ...
out.write(frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment