-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvi2au.py
executable file
·154 lines (123 loc) · 5.57 KB
/
vi2au.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
import os
from sys import argv
from glob import glob
from shutil import move
from logging import (basicConfig, debug, info, warning, DEBUG)
# Adding directory to cache
root = os.path.expanduser('~')
cache_dir = root + '/.cache/video-to-audio'
if not (os.path.exists(cache_dir)):
os.mkdir(root+'/.cache/video-to-audio')
log_file = os.path.join(cache_dir, 'info.log')
basicConfig(format="%(levelname)s:%(message)s", level=DEBUG, filename=log_file)
class Collector():
"""
Usage:
python3 [vi2au.py] [-d DIR [DIR ...]] [-m MEDIA_NAME [MEDIA_NAME ...]] [-o OUTPUT_DIR]
python3 [vi2au.py] [-d DIR] [-f FORMAT [FORMAT ...]] [-o OUTPUT_DIR]
Options and arguments (and corresponding environment variables):
-h, --help Show this help message.
-d DIR [DIR ...] Convert specified/all files in a specified directory.
(if none specified => defaults to current directory)
-f FORMAT [FORMAT ...] Convert specific format media.
-m MEDIA_NAME [MEDIA_NAME ...] Provide media name.
-o OUTPUT_DIR Output directory.
"""
def __init__(self):
self.clear = ('cls' if os.name == 'nt' else 'clear')
self.current_dir = os.getcwd()
self.__formats__ = self.collect_spec('-f', '*')
self.__files__ = self.collect_spec('-m')
self.__dirs__ = self.collect_spec('-d')
self.__outdir__ = self.collect_spec('-o', '~/Downloads')
self.collection = []
self._garbage = []
self.collect_files()
self.FILES = len(self.collection)
def __str__(self):
return str(self.collection)
def collect_spec(self, spec, non_empty=''):
"""
Collects specifier arguments from provided sys.argv
and return list of arguments in given space.
spec : (string) => ('-f', '-m', '-d', '-o')
non_empty : (string/char) => if no such spec found, return non empty list [string/char]
"""
# Collect 'spec' arguments from sys.argv
collection = []
if spec in argv:
for arg in argv[argv.index(spec) + 1:]:
# Append till next specifier
if not arg.startswith('-'):
collection.append(arg)
else:
break
if non_empty and not collection:
collection.append(non_empty if (non_empty == '*') else os.path.expanduser(non_empty))
return collection
def _get_files(self, dir):
# Invalid path
if not os.path.exists(dir): return
os.chdir(dir)
for file_format in self.__formats__:
for file in glob('*.{}'.format(file_format)):
self.collection.append(os.path.join(dir, file))
os.chdir(self.current_dir)
def collect_files(self):
# Collect specified file(s) in specified dir(s)
if self.__dirs__ and self.__files__:
for dir in self.__dirs__:
for file in self.__files__:
self.collection.append(os.path.join(dir, file))
# Collect all file(s) in specified dir(s)
elif self.__dirs__:
for dir in self.__dirs__:
self._get_files(dir)
# Collect specified file(s) in current dir(s)
elif self.__files__:
for file in self.__files__:
self.collection.append(os.path.join(self.current_dir, file))
# Collect all file(s) in current dir(s)
elif not self.__dirs__ and ('-d' in argv or '-f' in argv):
self._get_files(self.current_dir)
def mov_to_dir(self, audio_dir, ext):
if not os.path.exists(audio_dir):
os.mkdir(audio_dir)
for audio_file in glob('*.{}'.format(ext)):
os.system(self.clear)
debug("Moving file '{}' to {}\ ".format(audio_file, audio_dir))
move(audio_file, audio_dir)
class VideoToMp3(Collector):
def __init__(self):
super().__init__()
os.system(self.clear)
debug("Files found = {}".format(self.FILES))
def __str__(self):
return super().__str__()
def splitName(self, filename):
path = os.path.dirname(filename)
file = os.path.basename(filename)
filename, extention = os.path.splitext(file)
return (path, filename, extention)
def convert(self, out_extention='mp3'):
for file in self.collection:
info('Converting file = {}'.format(file))
path, filename, extention = self.splitName(file)
debug("{} | {} | {}".format(path, filename, extention))
command = 'ffmpeg -i "' + file + '" "' + filename+'.'+out_extention + '"'
debug("command = " + command)
os.system(command)
for dir in self.__outdir__:
debug("Moving {} file(s) to '{}'".format(out_extention, dir))
self.mov_to_dir(dir, out_extention)
if __name__ == "__main__":
args = ('-m', '-h', '--help', '-f', '-d', '-o')
if not any([arg for arg in argv if arg in args]):
print("Usage: vi2au [-m MEDIA_NAME [MEDIA_NAME ...]]\n")
print("vi2au: error: You must provide at least one MEDIA_NAME.")
print("Type vi2au -h or --help to see a list of all options.")
else:
converter = VideoToMp3()
converter.convert()
if '-h' in argv or '--help' in argv:
help(VideoToMp3)