-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
add scripts to convert video into images and vice-versa #5006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import cv2 | ||
import os | ||
|
||
def convert_images_to_video(image_folder, image_extension, video_name): | ||
''' | ||
Converts images into a video. Given a folder name, the images located in there will be converted into a video file. | ||
|
||
Parameters | ||
---------- | ||
image_folder : str | ||
Path where the images are stored. | ||
image_extension: str | ||
The search for images will be performed by searching the files by their extension. | ||
video_name : str | ||
Path where the video will be created. | ||
''' | ||
images = [img for img in os.listdir(image_folder) if img.endswith(image_extension)] | ||
images.sort(key= lambda f: int(''.join(filter(str.isdigit, f)))) | ||
|
||
frame = cv2.imread(os.path.join(image_folder, images[0])) | ||
height, width, layers = frame.shape | ||
|
||
video = cv2.VideoWriter(video_name, 0, 25, (width,height)) | ||
|
||
for image in images: | ||
video.write(cv2.imread(os.path.join(image_folder, image))) | ||
|
||
cv2.destroyAllWindows() | ||
video.release() | ||
|
||
def convert_video_to_images(image_folder, video_path, image_extension = ".jpg"): | ||
''' | ||
Converts video into images. Given a video folder name, the video will be converted into images and stored at given folder location. | ||
|
||
Parameters | ||
---------- | ||
image_folder : str | ||
Path where the images will be stored. | ||
video_path : str | ||
Path where the video are store. | ||
image_extension: str | ||
The frames obtained from the video will be stored at given extension. JPG by default. | ||
''' | ||
vidcap = cv2.VideoCapture(video_path) | ||
success, image = vidcap.read() | ||
count = 1 | ||
|
||
while success: | ||
cv2.imwrite(f"{image_folder}{count}{image_extension}", image) | ||
success, image = vidcap.read() | ||
print('Read a new frame: ', success) | ||
count += 1 | ||
|
||
# convert_video_to_images("teste/", "teste.mp4") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add type hints for your functions