Firstly, i am sorry if the question is stupid. I'm beginner with python opencv and image processing.
I created a program in order to read the video as the input and save it into the pgm format.
import cv2
vidcap = cv2.VideoCapture('input_video.mp4')
success,image = vidcap.read()
count = 0
while success:
cv2.imwrite("frame%d.pgm" % count, image) # save frame as PGM file
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
vidcap.release()
After running i have the frames which seem not exactly the format of PGM image. The "magic number" is "F6" instead of "F5" of PGM format. As i know the "magic number" of PPM format (it maybe call PGM 'color', a lowest common denominator color image file format) is "F6". I don't understand why i cannot have exactly PGM image in this case. Can someone figure me out this question and give me a solution to have the PGM format frames in output ?
The video (I don't know how to upload video here, so i attack the link). I tested with this video and even with its gray-scale version but the "magic number" is always "F6".
In SO, i found this link that may be similar to my question but it is for C++.
Your image will be colour, so you will need to convert to greyscale before you can save it as PGM - meaning "Portable Grey Map":
# Convert to grey
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Write to disk
cv2.imwrite("frame%d.pgm" % count, gray)
By the way, IMHO it is normally a bad idea to repeat code that is doing the same thing in different places - it makes a maintenance nightmare. Rather than this where you do vidcap.read() twice:
success,image = vidcap.read()
...
while success:
cv2.imwrite("frame%d.pgm" % count, image)
success,image = vidcap.read()
consider using this so you only read the video in one place and there is only one place to add/change parameters:
while True:
success,image = vidcap.read()
if not success:
break
cv2.imwrite("frame%d.pgm" % count, image)
Related
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")
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 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
Disclaimer : I have no experience in computer vision or image processing, but I need to process videos to obtain data for machine learning.
I wish to read a greyscale movie (I made it using greyscale images) - frame by frame using moviepy. For further processing, I need greyscale frames. Here is my code:
clip = VideoFileClip('movie.mp4')
count =1
for frames in clip.iter_frames():
print frames.shape
count+=1
print count
The frame shapes come out to be (360L, 480L, 3L) while I was expecting (360L, 480L). And this puzzles me. Is there a way to get the "expected" shape? Python OpenCV ideas may work too, but I would prefer moviepy.
If your are dealing with videos and images, OpenCV is your friend:
import cv2
from moviepy.editor import VideoFileClip
clip = VideoFileClip('movie.mp4')
count =1
for frames in clip.iter_frames():
gray_frames = cv2.cvtColor(frames, cv2.COLOR_RGB2GRAY)
print frames.shape
print gray_frames.shape
count+=1
print count
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