I'm new to this community and I have a question about ffmpeg. I'm making a python script to concatenate videos (especially mp4) using ffmpeg and I also used multiprocessing to speed up the process.
this is the function to read and process the video file:
def process_video(group_number):
num_processes = mp.cpu_count()
width, height, frame_count = get_video_frame_details(file_name)
frame_jump_unit = frame_count // num_processes
# read video file
clip = cv.VideoCapture(file_name)
clip.set(cv.CAP_PROP_POS_FRAMES, frame_jump_unit * group_number)
# get height, width and frame count of the video
width, height = (
int(clip.get(cv.CAP_PROP_FRAME_WIDTH)),
int(clip.get(cv.CAP_PROP_FRAME_HEIGHT))
)
no_of_frames = int(clip.get(cv.CAP_PROP_FRAME_COUNT))
fps = int(clip.get(cv.CAP_PROP_FPS))
proc_frames = 0
# Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc('m', 'p', '4', 'v')
out = cv.VideoWriter()
output_file_name = "Aktorik_sliced.mp4"
out.open(output_file_name, fourcc, fps, (width, height), True)
out.open("output_{}.mp4".format(group_number), fourcc, fps, (width,
height), True)
try:
while proc_frames < frame_jump_unit:
ret, frame = clip.read()
if not ret:
break
except:
# Release resources
clip.release()
out.release()
# Release resources
clip.release()
out.release()
This is the function to concatenate the videos:
def combine_output_files(num_processes):
output_file_name = "Aktorik_sliced.mp4"
# Create a list of output files and store the file names in a txt file
list_of_output_files = ["output_{}.mp4".format(i) for i in
range(num_processes)]
with open("list_of_output_files.txt", "w") as f:
for t in list_of_output_files:
f.write("file {} \n".format(t))
# use ffmpeg to combine the video output files
ffmpeg_cmd = "ffmpeg -y -loglevel error -f concat -safe 0 -i
list_of_output_files.txt -vcodec copy " + output_file_name
sp.Popen(ffmpeg_cmd, shell=True).wait()
# Remove the temperory output files
# for f in list_of_output_files:
# remove(f)
# remove("list_of_output_files.txt")
And this is the function to call the multiprocess:
def multi_process():
num_processes = mp.cpu_count()
width, height, frame_count = get_video_frame_details(file_name)
print("Video processing using {} processes...".format(num_processes))
start_time = time.time()
# Parallel the execution of a function across multiple input values
p = mp.Pool(num_processes)
p.map(process_video, range(num_processes))
combine_output_files(num_processes)
end_time = time.time()
total_processing_time = end_time - start_time
print("Time taken: {}".format(total_processing_time))
print("FPS : {}".format(frame_count/total_processing_time))
In the end, this is what I got as an output:
Output file #0 does not contain any stream
I've tried many things with this code, but I still got no clue where did I do wrong. It will be such a big help if somebody can help me :)
Thanks in advance!
Related
I was trying to save video from an streaming URL for every 1 min duration and the filename will increment.
so the cam is streaming I have loaded the video with cv2 and start writing the first file ,after 5s the file will save and another file will create and the frame will write on this file, file name should increment ex. "filename1.avi", "filename2.avi" etc.
But the video is save on same file and didn't create another video file.
def show_video_stream(rtsp_address: str):
"""Visualize stream given an rtsp address.
Args:
rtsp_address (str): IP address with rtsp protocol.
Example: 'rtsp://{user}:{password}#{IP}:{port}'
"""
current_time_in_second = int(time.time())
cap = cv2.VideoCapture(rtsp_address)
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
size = (frame_width, frame_height)
frame_name = rtsp_address.split('#')[1]
cv2.namedWindow(frame_name, cv2.WINDOW_NORMAL)
video_count = 0 #here result is should be dynamic so new file should initiate after every 5s.
result = cv2.VideoWriter('video_live_feed' + str(video_count) + ".avi",
cv2.VideoWriter_fourcc(*'MJPG'),
5, size)
while (True):
ret, frame = cap.read()
cv2.imshow(frame_name, frame)
# save_one_second_file(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
result.write(frame)
after_time_in_second = int(time.time())
print(current_time_in_second)
print(after_time_in_second)
#calculating the time of the video for 5 s
if after_time_in_second - current_time_in_second == 5:
current_time_in_second = after_time_in_second
video_count = video_count + 1
print("release korlam")
result.release()
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(show_video_stream, ip_pool)
I have used result.release() to release the write of the cap, and increment the value of the video_count variable.
I want to save video file duration 5s from an streaming video. if the video file is 15s long i will have 3 video file with 5s duration.
At the moment you only open 1 video file, with a name corresponding to video_count == 0, just before the main capture loop.
When you detect that 5 seconds have passed you increment the video_count but do not open a new video file.
I advise you to add a function to do that, and call it whenever you need to open a new file:
def open_writer(video_count):
return cv2.VideoWriter('video_live_feed' + str(video_count) + ".avi",
cv2.VideoWriter_fourcc(*'MJPG'),
5, size)
You can use it before the capture loop:
result = open_writer(video_count)
As well as after releasing the previous writer every 5 seconds:
video_count = video_count + 1
result.release()
result = open_writer(video_count) # reopen the writer with a new filename
In addition you can consider to change the 5 seconds condition to:
#------------------------------------------------vv---
if after_time_in_second - current_time_in_second >= 5:
This will make your code more robust to missing the exact second (even though at reasonable capture FPS it will probably happen rarely if at all).
I have a list of videos (10 sec each) in a folder and I'm trying to loop through each action video to extract keypoints and save them as json files.
path = "path to video folder"
for file in os.listdir(path):
cap = cv2.VideoCapture(path+file)
while cap.isOpened():
try:
ret, frame = cap.read()
I ran into a problem where the extracted data has some keypoints from other videos, and I just want to run this code, end with the stop time for the video is done, pause, start next video. How can I help correct this?
If you want to process multiple videos in turn you can check the ret (success) value of cap.read() to detect the end of each file. Here is a basic example you can start with:
import os
import cv2
path = "videos"
for file in os.listdir(path):
print(file)
cap = cv2.VideoCapture(path + '/' + file)
count = 0
while True:
ret, frame = cap.read()
# check for end of file
if not ret:
break
# process frame
count += 1
print(f"{count} frames read")
cap.release()
In my code i'm looping over frames of a video, and trying to generate another mp4 video.
This is my code:
cap = cv2.VideoCapture(args.video)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))
while cap.isOpened():
ret, img = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
out.release()
break
#<code>...
#<code>...
print(type(my_image))
out.write(my_image)
The output of print(type(my_image)) is numpy.ndarray for each frame. When I ran the code, i got output_video.mp4 file, but weights only 300 kb (it needs to be about 50 mb).
I tried to save each frame as an image, and to see if it will work, and it did. This is the code:
img = Image.fromarray(my_image, 'RGB')
img.save('frameeeee-%s.png'%i)
I coded this function to solve a similiar problem, you need to save the images singularly into a folder and then you can use frames2video to convert it into a video.
def frames2video( path_in = "/content/original_frames" , path_out = "/content/outputvideo",
frame_rate = 30 , video_name="output_video" ):
"""
Given an input path to a folder that contains a set of frames, this function
convert them into a video and then save it in the path_out.
You need to know the fps of the original video, are 30 by default.
"""
img_path_list = natsorted(os.listdir(path_in))
assert(len(img_path_list)>0)
img_array = []
print("[F2V] Frames to video...", end="\n\n")
with tqdm(total=len(img_path_list)) as pbar:
for count,filename in enumerate(img_path_list):
img = cv2.imread(path_in+"/"+filename)
if(img is None):break
height, width, layers = img.shape
img_array.append(img)
size = (width,height)
pbar.update()
if os.path.exists(path_out): shutil.rmtree(path_out)
os.mkdir(path_out)
out = cv2.VideoWriter(path_out+"/"+str(video_name)+'.mp4', cv2.VideoWriter_fourcc(*'DIVX'), frame_rate, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
print("\n[F2V] Video made from "+str(count+1)+" frames", end="\n\n")
For completeness, i post also the viceversa, a function that given a video extract the frames.
def n_frames(video):
"""
Given an input video returns the EXACT number of frames(CV2 was not precise)
"""
success = True
count = 0
while success:
success,image = video.read()
if success == False: break
count+=1
return count
def video2frames( path_in = "/content/video.mp4" , path_out = "/content/original_frames",
n_of_frames_to_save = 999999, rotate=True, frames_name = "OrigFrame" ):
"""
Given a video from path_in saves all the frames inside path_out.
The number of frames(in case of long videos) can be truncated with
the n_of_frames_to_save parameter. Rotate is used to save rotated
frames by 90 degree. All the frames are named frames_name with an
index
"""
blur_threshold = 0
if os.path.exists(path_out): shutil.rmtree(path_out)
os.mkdir(path_out)
count = 0
success = True
vidcap = cv2.VideoCapture(path_in)
v2 = cv2.VideoCapture(path_in)
fps = vidcap.get(cv2.CAP_PROP_FPS)
if(fps>120):
print("CAP_PROP_FPS > 120, probabily you are using a webcam. Setting fps manually")
fps = 25
n_of_frames = n_frames(v2) # #int(video.get(cv2.CAP_PROP_FRAME_COUNT)) is not accurate, https://stackoverflow.com/questions/31472155/python-opencv-cv2-cv-cv-cap-prop-frame-count-get-wrong-numbers
if(n_of_frames_to_save < n_of_frames): n_of_frames = n_of_frames_to_save
print("[V2F] Dividing the video in " + str(n_of_frames) + " frames", end="\n\n")
for count in trange(n_of_frames):
success,image = vidcap.read()
if not success: break
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if(rotate): image = cv2.rotate(image,cv2.ROTATE_90_CLOCKWISE)
plt.imsave("%s/%s%d.png" % (path_out,frames_name+"_", count), image)
count+=1
print("\n[V2F] "+str(count)+" frames saved",end="\n\n")
return fps
Ok, I found a solution. I noticed that I had resize function in my code:
my_image = cv2.resize(image_before, (1280, 720))
So I changed
out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))
to
out = cv2.VideoWriter('outputttttt.mp4', fourcc, fps, (1280, 720))
And it works (:
I have a folder of pkl files in 'somefile' that I need to open as videos with opencv, but I keep getting the _pickle.UnpicklingError: unpickling stack underflow error. What am I doing wrong? I know that my code isn't pretty... Please don't roast me lol
import cv2 import os import pickle import numpy as np
subdir ='somefile' files = os.listdir(subdir)
# open pkl filesfor f in files:
with open(subdir + '/' + f, 'rb') as infile:
try:
unpickled_videos = pickle.load(infile)
for video in unpickled_videos:
print('{} has been unpickled'.format(os.path.abspath(video)))
# play video from file
for video in unpickled_videos:
if video == 'eye':
# create VideoCapture object, read from input file
cap = cv2.VideoCapture('eye' + '.mp4')
# check if camera opened successfully
if (cap.isOpened() == False):
print("Error opening {}".format(os.path.abspath(video)))
# convert resolutions from float to integer
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
# define codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(video + '.MP4', fourcc, fps, (frame_width, frame_height), True)
# read until video is completed
while True:
# capture frame-by-frame
ret, frame = cap.read()
# display resulting frame
cv2.imshow('frame', frame)
# press Q on keyboard to exit
if cv2.waitKey(0) & 0xFF == ord('q'):
break
except FileNotFoundError:
print('{} not found!'.format(f))
pass
except EOFError:
print('End of file error')
pass
#when everything done, release video capture object and close all frames
#cap.release() out.release() cv2.destroyAllWindows()
You could try to load it as a numpy array with:
import numpy as np
data = np.load(input_filename, allow_pickle=True)
The error sounds like it could be an issue with your data. In the case that some of your data is problematic, you could wrap the call in a try/except.
Hi i am trying to break a long video down into smaller videos. I got some code of the internet but when I run it it does not write the video what is wrong with my code?
I am not getting any errors.
import cv2
count = 0
if __name__ == '__main__':
vidPath = 'VideoNietBewerkt.mp4'
shotsPath = '/videos/%d.avi' % count
segRange = [(0,1000),(1000,2000),(2000,3000)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
for idx,(begFidx,endFidx) in enumerate(segRange):
writer = cv2.VideoWriter(shotsPath,fourcc,fps,size)
cap.set(cv2.CAP_PROP_POS_FRAMES,begFidx)
ret = True # has frame returned
while(cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
count += 1
The problem was that I did not closed my cap variable I fixed this by putting everything in the for loop
import cv2
vidPath = 'VideoNietBewerkt.mp4'
segRange = [(0,5000),(5000,50000),(50000,100400)] # <-- to fit my sample movie
for idx,(begFidx,endFidx) in enumerate(segRange):
cap = cv2.VideoCapture(vidPath) # <---- Open Cap
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc(*'jpeg'))
shotsPath = f'movie_{str(idx)}.avi' # <-- use idx for naming the output file
print(f'saving file: {shotsPath}')
writer = cv2.VideoWriter() # <-- instantiate the writer this way
writer.open(shotsPath, fourcc, fps, size) # <-- open the writer
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
while(cap.isOpened() and writer.isOpened()): # removed and ret
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
cap.release() #<--- Closed Cap
It seems like there is a problem with the codec (at least for me) and with the output filename, which is not updated outside the loop.
I made some changes for working on my machine, try this out with a short movie, there are few comments in the code itself.
This worked for me:
import cv2
vidPath = 'movie.mp4'
segRange = [(0,30),(30,60),(60,90)] # <-- to fit my sample movie
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc(*'jpeg')) # <-- I had to change the codec
for idx,(begFidx,endFidx) in enumerate(segRange):
shotsPath = f'movie_{str(idx)}.avi' # <-- update filename here, use idx for naming the output file
print(f'saving file: {shotsPath}')
writer = cv2.VideoWriter() # <-- instantiate the writer this way
writer.open(shotsPath, fourcc, fps, size) # <-- open the writer
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
while(cap.isOpened() and writer.isOpened()): # removed and ret
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
cap.release()