Created
November 1, 2018 21:04
-
-
Save ajayguru2/176d4d971fd803a769a1311a667ea252 to your computer and use it in GitHub Desktop.
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
''' | |
Using OpenCV takes a mp4 video and produces a number of images. | |
Requirements | |
---- | |
You require OpenCV 3.2 to be installed. | |
Run | |
---- | |
''' | |
import cv2 | |
import numpy as np | |
import os | |
import sys | |
# sys.path.insert( 0, 'D:\Super-Resolution-master\SR_Project\Application') | |
# import cnn | |
import moviepy.editor as mp | |
import subprocess | |
from os.path import isfile, join | |
import shutil | |
def handle_uploaded_file(testfile): | |
print(testfile) | |
#extracting audio from the file | |
command = "ffmpeg -i testfile -ab 160k -ac 2 -ar 44100 -vn ./audio.mp3" | |
subprocess.call(command, shell=True) | |
# Playing video from file: | |
cap = cv2.VideoCapture(testfile) | |
# print(type(testfile)) | |
try: | |
if os.path.exists('./output.mp4'): | |
os.remove('./output.mp4') | |
except OSError: | |
print('Output buffer already full with video/output.mp4') | |
try: | |
if os.path.exists('./video.mp4'): | |
os.remove('./video.mp4') | |
except OSError: | |
print('Output buffer already full with video/output.mp4') | |
try: | |
if not os.path.exists('data'): | |
os.makedirs('data') | |
except OSError: | |
print('Error: Creating directory of data') | |
currentFrame = 0 | |
while(True): | |
# Capture frame-by-frame | |
ret, frame = cap.read() | |
if not ret: break | |
# Saves image of the current frame in jpg file | |
name = './data/frame' + str(currentFrame) + '.png' | |
print('Creating...' + name) | |
cv2.imwrite(name, frame) | |
# To stop duplicate images | |
currentFrame += 1 | |
# When everything done, release the capture | |
cap.release() | |
cv2.destroyAllWindows() | |
# cnn.process() | |
def convert_frames_to_video(pathIn,pathOut,fps): | |
frame_array = [] | |
files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))] | |
#for sorting the file names properly | |
files.sort(key = lambda x: int(x[5:-4])) | |
for i in range(len(files)): | |
filename=pathIn + files[i] | |
#reading each files | |
img = cv2.imread(filename) | |
height, width, layers = img.shape | |
size = (width,height) | |
print(filename) | |
#inserting the frames into an image array | |
frame_array.append(img) | |
out = cv2.VideoWriter(pathOut, 0x00000021, fps, size) | |
for i in range(len(frame_array)): | |
# writing to a image array | |
out.write(frame_array[i]) | |
out.release() | |
def write_video(): | |
pathIn= './data/' | |
pathOut = './video.mp4' | |
fps = 20 | |
convert_frames_to_video(pathIn,pathOut,fps) | |
video = mp.VideoFileClip("./video.mp4") | |
video.write_videofile("./output.mp4", audio="./audio.mp3") | |
def delete_data(): | |
if os.path.exists("./data"): | |
os.rmdir("./data") | |
else: | |
print("The file does not exist") | |
def delete_video(): | |
if os.path.exists("./video.mp4"): | |
os.remove("./video.mp4") | |
else: | |
print("The file does not exist") | |
def delete_output(): | |
if os.path.exists("./output.mp4"): | |
os.remove("./output.mp4") | |
else: | |
print("The file does not exist") | |
def clear_media(): | |
shutil.rmtree('./media') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment