|
| 1 | +import cv2 |
| 2 | +import tkinter as tk |
| 3 | +from tkinter.filedialog import * |
| 4 | + |
| 5 | +window = tk.Tk() |
| 6 | +window.title("Video reversing") |
| 7 | +window.geometry('400x200') |
| 8 | +label = tk.Label(window, text="Input video file should be in the current folder").grid(row=0, column=0) |
| 9 | +label = tk.Label(window, text="Output/reversed video file is saved in the current folder").grid(row=1, column=0) |
| 10 | +label = tk.Label(window, text="Close this dialog box to proceed").grid(row=3, column=0) |
| 11 | +window.mainloop() |
| 12 | + |
| 13 | + |
| 14 | +cap = cv2.VideoCapture('sampleVideo.mp4') |
| 15 | + |
| 16 | +frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) |
| 17 | +fps = cap.get(cv2.CAP_PROP_FPS) |
| 18 | + |
| 19 | +height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) |
| 20 | +width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) |
| 21 | +size = (int(width*0.5), int(height*0.5)) |
| 22 | + |
| 23 | +# defining/writing the output video and its format |
| 24 | +fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
| 25 | +out = cv2.VideoWriter('sampleOutput.mp4', fourcc, fps, size) |
| 26 | + |
| 27 | +print("No. of frames: ", frames) |
| 28 | +print("FPS: ", fps) |
| 29 | + |
| 30 | +frameIdx = frames - 1 |
| 31 | +if(cap.isOpened()): |
| 32 | + # printing the progress |
| 33 | + print("Progress:\n") |
| 34 | + while(frameIdx!=0): # iterating from last frame to first |
| 35 | + cap.set(cv2.CAP_PROP_POS_FRAMES, frameIdx) # pointing to last frame |
| 36 | + ret, frame = cap.read() |
| 37 | + frame = cv2.resize(frame, size) |
| 38 | + frameIdx = frameIdx - 1 |
| 39 | + if(frameIdx%100 == 0): # progress updated every 100 frames |
| 40 | + print(frameIdx) |
| 41 | + out.write(frame) |
| 42 | + |
| 43 | +out.release() |
| 44 | +cap.release() |
| 45 | +cv2.destroyAllWindows() |
0 commit comments