-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
132 lines (100 loc) · 4.2 KB
/
main.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
import os
import subprocess
from halo import Halo
from pathlib import Path
from colorama import Fore
import pyfiglet
import random
from tqdm import tqdm
from utils import *
spinner = Halo(text='Verificando vídeos para conversão...', spinner='dots')
class Banner:
def __init__(self, banner):
self.banner = banner
self.lg = Fore.LIGHTGREEN_EX
self.w = Fore.WHITE
self.cy = Fore.CYAN
self.ye = Fore.YELLOW
self.r = Fore.RED
self.n = Fore.RESET
def print_banner(self):
colors = [self.lg, self.r, self.w, self.cy, self.ye]
f = pyfiglet.Figlet(font='slant')
banner = f.renderText(self.banner)
print(f'{random.choice(colors)}{banner}{self.n}')
print(f'{self.r} Version: v0.0.3 https://github.com/viniped/vidconverter \n{self.n}')
def get_codec(file_path, stream_type):
cmd = [
'ffprobe',
'-v', 'error',
'-select_streams', f'{stream_type}:0',
'-show_entries', 'stream=codec_name',
'-of', 'default=noprint_wrappers=1:nokey=1',
file_path
]
codec = subprocess.check_output(cmd).decode('utf-8').strip()
return codec
def convert_file(file_path):
video_codec = get_codec(file_path, 'v')
audio_codec = get_codec(file_path, 'a')
if video_codec == "h264" and audio_codec == "aac" and file_path.lower().endswith(".mp4"):
return
file_name, file_extension = os.path.splitext(file_path)
output_file = f"{file_name}.mp4"
cmd = [
'ffmpeg',
'-v', 'quiet',
'-stats',
'-y',
'-i', file_path,
'-b:a', '128k',
'-hide_banner'
]
if video_codec == "h264" and audio_codec == "aac":
cmd.extend(['-c:v', 'copy', '-c:a', 'copy'])
elif video_codec != "h264" and audio_codec == "aac":
cmd.extend(['-c:v', 'libx264', '-preset', 'ultrafast', '-threads', '2', '-c:a', 'copy', '-crf', '23', '-maxrate', '4M'])
elif video_codec == "h264" and audio_codec != "aac":
cmd.extend(['-c:v', 'copy', '-c:a', 'aac', '-preset', 'ultrafast', '-threads', '2', '-crf', '23', '-maxrate', '4M'])
elif video_codec != "h264" and audio_codec != "aac":
cmd.extend(['-c:v', 'h264', '-c:a', 'aac', '-preset', 'ultrafast', '-threads', '2', '-crf', '23', '-maxrate', '4M'])
else:
cmd.extend(['-c:v', 'h264', '-c:a', 'aac', '-preset', 'ultrafast', '-threads', '2', '-crf', '23', '-maxrate', '4M'])
cmd.append(output_file)
subprocess.run(cmd)
os.remove(file_path)
def convert_videos_in_folder(folder_path):
delete_videos_without_duration(folder_path)
spinner.start()
videos_to_convert = []
for subdir, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(tuple(video_extensions)):
file_path = os.path.join(subdir, file)
video_codec = get_codec(file_path, 'v')
audio_codec = get_codec(file_path, 'a')
if not (video_codec == "h264" and audio_codec == "aac" and file_path.lower().endswith(".mp4")):
videos_to_convert.append(file_path)
spinner.stop()
total_videos = len(videos_to_convert)
if total_videos == 0:
spinner.succeed("Todos os vídeos já estão no formato correto.")
return
else:
spinner.info(f"{total_videos} vídeos precisam ser convertidos.")
with tqdm(total=total_videos, desc="Convertendo vídeos") as pbar:
for index, video_path in enumerate(videos_to_convert):
convert_file(video_path)
pbar.update(1)
spinner.succeed("A conversão dos vídeos foi concluída.")
def main():
try:
banner = Banner('VidConverter')
banner.print_banner()
folder_path = input("Digite o caminho da pasta onde estão os vídeos: ")
delete_videos_without_duration(folder_path)
convert_videos_in_folder(folder_path)
except KeyboardInterrupt:
spinner.fail('Operação Interrompida pelo usuário')
if __name__ == '__main__':
main()