Skip to content

Instantly share code, notes, and snippets.

@manifoldhiker
Last active December 21, 2022 20:20
Show Gist options
  • Save manifoldhiker/8384a348a87ae555319c289353f7a5dd to your computer and use it in GitHub Desktop.
Save manifoldhiker/8384a348a87ae555319c289353f7a5dd to your computer and use it in GitHub Desktop.
from tqdm.auto import tqdm
import cv2
import torch
def read_video(file):
capture = cv2.VideoCapture(file)
fps = capture.get(cv2.CAP_PROP_FPS)
n_frames = capture.get(cv2.CAP_PROP_FRAME_COUNT)
frames = []
for _ in tqdm(np.arange(n_frames)):
success, image_cv = capture.read()
if not success:
break
frame = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
frame = torch.tensor(frame/255, dtype=torch.float32)
frames.append(frame.permute(2,0,1)[None])
return frames, fps, n_frames
@manifoldhiker
Copy link
Author

manifoldhiker commented Nov 21, 2022

# pip install moviepy --upgrade
from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
fps=25.
out_video_path = 'video.mp4'

frames = [(f*255).astype(np.uint8) for f in images]

clip = clip =ImageSequenceClip(frames, fps=fps)
clip.write_videofile(out_video_path, fps=fps)

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