-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.py
193 lines (154 loc) · 5.44 KB
/
converter.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import argparse
import sys
import os
import glob
import logging
import moviepy.editor as mp
import threading
from queue import Queue
VERSION = 'Version 1'
queue = Queue()
_FORMATS = ('mp3','mp4','webm')
DEST_PATH =''
FILES = []
def set_destination(destpath):
'''
Create a folder called converted to store the converted files
'''
if not os.path.exists("converted"):
print("creating....")
try:
os.makedirs(destpath+"/converted")
except FileExistsError:
print("That folder exists.")
DEST_PATH = destpath+"/converted"
print("setting destination folder")
print("Destination path is %r "%DEST_PATH)
return DEST_PATH
def begin_convertion(f):
'''
Each Thread uses this function to convert a file
'''
ct = threading.currentThread()
head, tail = os.path.split(f)
name,ext = tail.split(".")
print("Reading... %s" %name)
try:
clip = mp.VideoFileClip(f)
print("Converting ... %s" %name)
rawstring = ct._args[0]+"/"+name+".mp3"
formatedstr = rawstring.replace('\\','/')
# clip.audio.write_audiofile(formatedstr)
except Exception as e:
print("Error writting file")
print(e)
sys.exit(0)
# Create worker threads (will die when main exits)
def create_workers(threads,df):
print("Creating %r workers"%threads)
for _ in range(threads):
t = threading.Thread(target=work,args=(df,))
t.daemon = True
t.start()
# Do the next job in the queue
def work(x):
print("Issuing jobs")
while True:
task = queue.get()
begin_convertion(task)
queue.task_done()
# Each file in a folder is s a new job
def create_jobs(files):
for f in files:
queue.put(f)
queue.join()
def check_folder_exists(args):
'''This methods checks if the provided data folder exists '''
status = []
for path in args:
try:
if os.path.exists(path) is False:
status.append("Directory %r does not exist"%path)
else:
status.append(True)
except Exception as e:
status.append("Please check on slashes.")
continue
return status
def main():
"""
Program entry point
Start by fetching data from the Commandline
"""
parser = argparse.ArgumentParser(description="simple video converter commandline tool")
parser.add_argument('-v','--version', action='store_true', default=False,help='Displays version and exits.')
parser.add_argument('-t','--threads', action='store_true', help='Number of Concurrent conversions.')
parser.add_argument('-dp', '--datapath', help="The path to the data converted",type=str)
parser.add_argument('-dd', '--destinationdirectory', help="destination directory path, a new foder 'converted' will be created ",type=str)
parser.add_argument('-f', '--format', help="The desired output file format",choices=_FORMATS,type=str)
args = parser.parse_args()
print(args)
if args.version:
print(VERSION)
sys.exit(0)
if args.threads is False:
args.threads = 20
if args.datapath is None and args.destinationdirectory is None and args.format is None:
parser.print_usage()
sys.exit(0)
if args.datapath is None:
print("You need to provide a data directory path")
parser.print_usage()
sys.exit(0)
if args.destinationdirectory is None:
print("You need to provide a data destination directory path")
parser.print_usage()
sys.exit(0)
if args.format is None and args.format not in _FORMATS:
print("You cannot privide %r format" %args.format)
parser.print_usage()
sys.exit(0)
# cleaned data
datapath = args.datapath
destinationpath = args.destinationdirectory
format = args.format
NUMBER_OF_THREADS = args.threads
# check whether the provided paths are valid paths
paths = [datapath,destinationpath]
status = check_folder_exists(paths)
if status[0] is not True or status[1] is not True:
print(status)
sys.exit(0)
else:
# The folders are ok
print("ok")
# check if the data folder has data
arr = os.listdir(datapath)
# Fetch files with absolute path
absfilepath = [datapath+'/'+i for i in arr]
print("These are the files to be converted into %r format \n"%format)
x=1
for i in arr:
print(str(x)+"."+i)
print("\n")
_OPTIONS=('1','2',"yes","no")
SELECTING = True
try:
while SELECTING:
option = input("Sure to proceed? \n 1.Yes \n 2. No \n")
if option not in _OPTIONS:
print("Please select 1 or 2 or Yes or No \n")
else:
SELECTING=False
if option is '1' or option is 'yes':
print("Almost there .... \n")
df = set_destination(destinationpath)
create_workers(NUMBER_OF_THREADS,df)
create_jobs(absfilepath)
elif option is '2' or option is 'no':
print("Thanks for using this tool")
sys.exit(0)
except KeyboardInterrupt:
print("Thanks for using this tool")
if __name__ == '__main__':
main()