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
Related
I'm trying to use python to capture a photo from my ip cameras every 1 minute and save it as a file for later use.
I'm still not good at python and I don't get why some of the times I get the right image and sometimes I get a corrupted gray image.
I'm using hikvision api to get the rtsp stream and while the stream is working sometimes the images are still fully gray.
Here is the code I wrote:
import cv2
import time
count = 0
while True:
for x in range(1, 9):
count = count +1
RTSP_URL = f'rtsp://user:password#ip:port/ISAPI/Streaming/Channels/{x}01'
cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)
result, image = cap.read()
if result:
cv2.imwrite(f"pictures/{x}{count}.png", image)
time.sleep(60)
I would be happy to hear suggestions to find the best way to do this task.
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
I am developing a program in Python which creates a video from an HTML file.
The HTML file may contain one animated GIF, and I need to keep the animation in the final video.
I already managed to extract each frame from the GIF, inspired by the method provided in the following GitHub Gist:
https://gist.github.com/revolunet/848913
import os
from PIL import Image
def extractFrames(inGif, outFolder):
frame = Image.open(inGif)
nframes = 0
while frame:
frame.save( '%s/%s-%s.gif' % (outFolder, os.path.basename(inGif), nframes ) , 'GIF')
nframes += 1
try:
frame.seek( nframes )
except EOFError:
break;
return True
extractFrames('ban_ccccccccccc.gif', 'output')
I replaced the GIF source in the HTML with each of these frames, and generated all the frames of the final video using PhantomJS.
Now, I need to get the duration of each frame in the source GIF file, in order to reproduce it in the corresponding frame of the video.
I found no way to achieve this in Python.