-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertToMp4.py
85 lines (66 loc) · 3.27 KB
/
convertToMp4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python
import sys, os;
import argparse;
from os.path import expanduser;
from subprocess import run;
__author__ = "Jeetesh Mangwani"
def resizeFile(path, subdirs, file, args):
name, extension = os.path.splitext(file);
image_file_extensions = [".png", ".jpg", ".jpeg", ".PNG", ".JPG", ".JPEG"];
video_file_extensions = [".avi", ".mp4", ".AVI", ".MP4", ".mov", ".MOV"];
if(extension in image_file_extensions):
resizeImageFile(path, subdirs, file, args);
elif(extension in video_file_extensions):
resizeVideoFile(path, subdirs, file, args);
else:
print("Ignoring the file: ", os.path.join(path, file));
def resizeImageFile(path, subdirs, file, args):
size = args.imagesize;
verbose = args.verbose;
filePath = os.path.join(path, file);
relativeFilePath = os.path.relpath(filePath, args.inputdirectory);
relativeDirectoryPath = os.path.relpath(path, args.inputdirectory);
destinationFilePath = os.path.join(args.outputdirectory, relativeFilePath);
destinationDirectoryPath = os.path.join(args.outputdirectory, relativeDirectoryPath);
#print("Resizing to Size: {1} Input: {0} Output: {2}".format(filePath, size, destinationFilePath));
if args.imagesize == "small":
outputWidth=640 #will be used if aspect ratio > 1
outputHeight=411 #will be used if aspect ratio <= 1
outputSize="50"
elif args.imagesize == "medium":
outputWidth=800 #will be used if aspect ratio > 1
outputHeight=600 #will be used if aspect ratio <= 1
outputSize="200"
else:
outputWidth=1000 #will be used if aspect ratio > 1
outputHeight=750 #will be used if aspect ratio <= 1
outputSize="1000"
#using exact path because there is another program called "convert" in Windows
command = ["C:\Program Files\ImageMagick-7.0.9-2-portable-Q16-x64\convert.exe", filePath, "-define", "jpeg:extent=" + outputSize + "kb", "-resize", str(outputWidth) + "x" + str(outputHeight), destinationFilePath];
print("Command: ", " ".join(command));
if not os.path.exists(destinationDirectoryPath):
print("Creating directory: " + destinationDirectoryPath);
os.makedirs(destinationDirectoryPath);
run(command);
def resizeVideoFile(path, subdirs, file, args):
size = args.videosize;
verbose = args.verbose;
filePath = os.path.join(path, file);
relativeFilePath = os.path.relpath(filePath, args.inputdirectory);
relativeDirectoryPath = os.path.relpath(path, args.inputdirectory);
destinationFilePath = os.path.join(args.outputdirectory, relativeFilePath) + ".mp4";
destinationDirectoryPath = os.path.join(args.outputdirectory, relativeDirectoryPath);
fps="24";
print("Resizing to Size: {1} Input: {0} Output: {2}".format(filePath, size, destinationFilePath));
if args.imagesize == "small":
outputWidth=640
elif args.imagesize == "medium":
outputWidth=800
else:
outputWidth=1000
command = ["ffmpeg", "-i", filePath, "-q:v", "0", destinationFilePath];
print("Command: ", " ".join(command));
if not os.path.exists(destinationDirectoryPath):
print("Creating directory: " + destinationDirectoryPath);
os.makedirs(destinationDirectoryPath);
run(command);