Read frame by frame and create video using cv2 in python - python

I want to read frame by frame from existing video, and create new video from these frames.
In my real project I want to change each frame before making the new video but for simplicity's sake of this question I just want to create new video from the same frames without any change.

This code is working for me:
import cv2
vidcap = cv2.VideoCapture('input_video.mp4')
vidwrite = cv2.VideoWriter('output_video.mp4', cv2.VideoWriter_fourcc(*'MP4V'), 30, (1920,1080))
success,image = vidcap.read()
while success:
vidwrite.write(image) # write frame into video
success,image = vidcap.read() # read frame from video
vidwrite.release()
cv2.destroyAllWindows()
The strange thing is that the output video is twice bigger than the input video

Related

Not able to run cv2.VideoCapture()

I have a dataset containing several videos in .mp4 format stored in a source directory. I want to resize the frames of every video and store in destination directory. I am running a for loop to get every video from source and extract each frame, resize and write in a video and store in destination. But I'm stuck at the VideoCapture part. As far as my knowledge the path is correctly given. Here is the code and the error
`import cv2
import numpy as np
import os
source="C:\\Users\\Desktop\\sourcedir"
dest=r"C:\Users\Desktop\dest"
def frame_capture(file):
path=dest+'\\'+file
so=source+'\\'+file
result = cv2.VideoWriter(path, cv2.VideoWriter_fourcc(*'mp4v'), 25, (227,227))
cap = cv2.VideoCapture(so)
if(cap.isOpened()==False):
print("Unable to open file")
currentFrame = 0
while(True):
# Capture frame by frame
ret, frame = cap.read()
# resize frame
frame=cv2.resize(frame,(227,227))
result.write(frame)
# To stop duplicate images
currentFrame += 1
cap.release()
cv2.destroyAllWindows()
for file in os.listdir(source):
frame_capture(file)
`
OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize
Need help

I need to play audio in sync with frames extracted from a video

i am a beginner in coding and i need to play audio in sync with frames extracted from a video.
I already extracted all the frames from the video and the audio too.
and now i'm stuck .I need help to make a python app, that will 'play' the images along with the audio, and make sure it is synchronized.
the program should measure time. Time will start at the begging of the playback, audio will start playing at the begging of the playback.
Then use the current time and framerate to get the appropriate image and show it on screen.
find below what i have done so far .
Thank you in advance .
import cv2
import moviepy.editor as mp
#Extracting frames for a video and saving those frames.
vidcap = cv2.VideoCapture('video_tiav.mp4')
success,image = vidcap.read()
count = 0
while success:
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
#Extracting audio for a video and saving .
video = mp.VideoFileClip(r'video_tiav.mp4') #'r' indicates that we are reading a file
video.audio.write_audiofile(r"output.mp3")

Capture Random Frame from an Online hosted MP4 file

Here, I have a website where I am scraping MP4 files, my problem is that I need to generate normal thumbnails with specific measurements, I searched for the last and tried many things, nothing worked.
I want something fast and can randomly pick a frame from half the video or the duration I provide.
I found thumb-gen, but I think it doesn't support online usage...
Just found the solution:
import cv2
vidcap = cv2.VideoCapture(MP4URL)
success,image = vidcap.read()
count = 0
while success:
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1

i want to collect the current time of a video

I found a way to extract a frame from a video, using cv2.
but What i also want to achieve is: (press pause -while the video is playing- so the frame is captured and the current time of the video is taken, So I can make a comment or whatever)
my code for extracting the frame
import cv2
vidcap = cv2.VideoCapture('d:/final.mp4')
vidcap.set(cv2.CAP_PROP_POS_MSEC,25505) # just cue to 20 sec. position
success,image = vidcap.read()
if success:
cv2.imwrite("frame22sec.jpg", image) # save frame as JPEG file
cv2.imshow("20sec",image)
cv2.waitKey()
I failed in my research (8 hours), please direct me to a proper method
I am using python34

Editing video frames on Raspberry Pi

I have the following code:
# Import packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
X_RESOLUTION = 640
Y_RESOLUTION = 480
# Initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (X_RESOLUTION, Y_RESOLUTION)
camera.framerate = 10
rawCapture = PiRGBArray(camera, size = (X_RESOLUTION, Y_RESOLUTION))
# Allow camera to warmup
time.sleep(0.1)
#Capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# Grab the raw NumPy array representing the image
image = frame.array
# Show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# Clear the stream so it is ready to receive the next frame
rawCapture.truncate(0)
# If the 'q' key was pressed, break from the loop
if(key == ord('q')):
break
It is all fine and dandy. It captures video and displays it on my screen and it exits when I press 'q'. However, if I wanted to manipulate the frames somehow, say for example I wanted to set every pixels R value in each frame to 255 to make the image red. How would I do that?
My end goal is to write software that detects movement on a static background. I understand the theory and the actual data manipulation that needs to be done to make this happen, I just cant figure out how to access each frame's pixel data and operate on it. I attempted to change some values in 'image', but it says the array is immutable and cannot be written to, only read from.
Thanks for your time.
I have accessed each pixel{R,G,B values accessed separately } value randomly and have changed the value of it in the image. You can do it on an video by extracting each frame of it. It is implemented in c++ with opencv. Go through this link https://stackoverflow.com/a/32664968/3853072 you will get an idea.

Categories