-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrop.py
More file actions
152 lines (130 loc) · 5.68 KB
/
crop.py
File metadata and controls
152 lines (130 loc) · 5.68 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
@author: Aljoscha Lipski
@mail: aljoscha.lipski@googlemail.com
@license: MIT
Copyright (c) 2023 Aljoscha Lipski
"""
import sys
import os
import subprocess
# global variables
OUTPUT_FOLDER_NAME = 'croppedVideos'
def cropSingleVideo(nameVideo: str, pathVideo: str, cropLeft: int, cropRight: int,
cropTop: int, cropBottom: int):
'''
crops a video to the given size.
arguments:
- name of the video
- path to the video
- percentage of cropping left edge
- percentage of cropping right edge
- percentage of cropping from the top edge
- percentage of cropping from the bottom edge
'''
# get the video size
size = subprocess.run(['ffprobe', '-y', 'error', '-show_entries',
'stream=width,height', '-of', 'default=noprint_wrappers=1',
pathVideo], capture_output=True, text=True)
width = int((size.stdout.split('=')[1]).split('\n')[0])
height = int(size.stdout.split('=')[2])
# calculate the percentages in position and pixels.
# see: http://ffmpeg.org/ffmpeg-all.html#crop
left = width * (cropLeft / 100)
top = width * (cropTop / 100)
right = width - (width * (cropRight / 100)) - left
bottom = height - (height * (cropBottom / 100)) - top
subprocess.run(['ffmpeg', '-i', pathVideo,
'-filter:v', 'crop=' + str(right) + ':' + str(bottom) + ':' +
str(left) + ':' + str(top),
OUTPUT_FOLDER_NAME + '/' + nameVideo])
def cropAllVideos(pathFolder: str, cropLeft: int, cropRight: int, cropTop: int,
cropBottom: int):
'''
This function iterates through the given folder and crops all .mp4 files.
Those new files will be written into a new folder.
Every other content of the original folder will be copied to the new folder.
arguments:
- path to the folder with the videos
- percentage of cropping left edge
- percentage of cropping right edge
- percentage of cropping from the top edge
- percentage of cropping from the bottom edge
'''
if os.name == "nt":
linux = False
subprocess.run(['lmkdir', OUTPUT_FOLDER_NAME])
else:
linux = True
subprocess.run(['mkdir', OUTPUT_FOLDER_NAME])
counter = 0
# iterate through the folder and crop every .mp4 file
for filename in os.listdir(pathFolder):
pathFile = os.path.join(pathFolder, filename)
# checking if it is a file
if os.path.isfile(pathFile):
# if it is .mp4 or .webm crop the video
if (filename.endswith('.mp4') or filename.endswith('.webm')):
cropSingleVideo(filename, pathFile, cropLeft, cropRight,
cropTop, cropBottom)
# otherwise just copy it to the new folder
else:
if linux:
subprocess.run(
['cp', pathFile, OUTPUT_FOLDER_NAME + '/' + filename])
else:
subprocess.run(
['copy', pathFile, OUTPUT_FOLDER_NAME + '/' + filename])
counter += 1
print("DONE: " + str(counter))
if __name__ == "__main__":
helpText = "Please use the script like this:\n\npython3 crop.py path/to/video/folder cropLeft cropRight cropTop cropBottom\n\n\
Important:\n\
- Only .mp4 or .webm videos will be taken, all other files will be skipped\n\
- This script is only tested for Linux but should work on Windows as well if ffmpeg is installed\n\
- cropXY must be an integer between 0 and 100\n\
- the sum of cropLeft and cropRight must be < 100\n\
- the sum of cropTop and cropBottom must be < 100\n\n\
Usage:\n\
cropXY is given in percentage from the position X.\n\
So if cropLeft = 10, cropRight = 20, cropTop = 0, cropBottom = 50,\n\
that means the cropped window is 10% from the left edge, \
20% from the right edge,\n\
0% from the top edge (so nothing) and 50% from the bottom edge away,\n\
The reference is the size of the original video size."
if len(sys.argv) == 1:
print(helpText)
sys.exit(0)
# check for help
if (sys.argv[1] == "-h" or sys.argv[1] == "--h" or
sys.argv[1] == "-help" or sys.argv[1] == "--help"):
print(helpText)
sys.exit(0)
# check for correct amount of arguments
if len(sys.argv) != 6:
print("\nThe correct amount of arguments was not given!")
print(helpText)
sys.exit(0)
# test the percentage arguments on being a number
if (not sys.argv[2].isnumeric() or not sys.argv[3].isnumeric() or
not sys.argv[4].isnumeric() or not sys.argv[5].isnumeric()):
print("\nSome percentage arguments are no positive integers!\n\n")
print(helpText)
sys.exit(0)
# test the percentage arguments on the range
if (int(sys.argv[2]) + int(sys.argv[3]) >= 100 or
int(sys.argv[4]) + int(sys.argv[5]) >= 100 or
int(sys.argv[2]) >= 100 or int(sys.argv[3]) >= 100 or
int(sys.argv[4]) >= 100 or int(sys.argv[5]) >= 100 or
int(sys.argv[2]) < 0 or int(sys.argv[3]) < 0 or
int(sys.argv[4]) < 0 or int(sys.argv[5]) < 0
):
print("\nThe sum of some percentages is >= 100 or some percentages are larger than 100!\n\n")
print(helpText)
sys.exit(0)
# check if the path exists
if not os.path.exists(sys.argv[1]):
print("\nThe provided path does not exist or is wrong!\n\n")
print(helpText)
sys.exit(0)
cropAllVideos(sys.argv[1], int(sys.argv[2]),
int(sys.argv[3]), int(sys.argv[4]), int(sys.argv[5]))