-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
executable file
·279 lines (244 loc) · 8.5 KB
/
convert.py
File metadata and controls
executable file
·279 lines (244 loc) · 8.5 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# A script to convert video files to audio files using FFmpeg.
import os
import subprocess
import threading
import time
from pathlib import Path
import logging
import sys
# Logging configuration
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.FileHandler(
"conversion.log"), logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger(__name__)
class VideoToAudioConverter:
def __init__(
self,
bitrate="192k",
samplerate=44100,
output_dir=None,
delete_original=False,
overwrite=False,
output_format="mp3",
):
self.bitrate = bitrate
self.samplerate = samplerate
self.output_dir = output_dir
self.delete_original = delete_original
self.overwrite = overwrite
self.output_format = output_format.lower()
# Supported formats
self.supported_input_formats = {
".mp4",
".mkv",
".avi",
".mov",
".wmv",
".flv",
".webm",
}
self.supported_output_formats = {"mp3", "wav"}
if output_dir:
Path(output_dir).mkdir(parents=True, exist_ok=True)
def check_ffmpeg_installed(self):
try:
subprocess.run(["ffmpeg", "-version"],
capture_output=True, check=True)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
return False
def get_video_duration(self, file_path):
try:
cmd = [
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
str(file_path),
]
result = subprocess.run(
cmd, capture_output=True, text=True, check=True)
return float(result.stdout.strip())
except Exception:
return 0
def convert_video_to_audio(self, input_file, progress_callback=None):
input_path = Path(input_file)
# Check if input format is supported
if input_path.suffix.lower() not in self.supported_input_formats:
logger.error(f"Formato input non supportato: {input_path.suffix}")
logger.error(
f"Formati supportati: {
', '.join(self.supported_input_formats)}"
)
return False
# Check if output format is supported
if self.output_format not in self.supported_output_formats:
logger.error(f"Formato output non supportato: {
self.output_format}")
logger.error(
f"Formati supportati: {
', '.join(self.supported_output_formats)}"
)
return False
# Determine output path
if self.output_dir:
output_path = Path(self.output_dir) / (
input_path.stem + f".{self.output_format}"
)
else:
output_path = input_path.with_suffix(f".{self.output_format}")
if output_path.exists() and not self.overwrite:
logger.warning(f"{output_path} exists, skipping.")
return False
duration = self.get_video_duration(input_file)
# Build FFmpeg command based on output format
if self.output_format == "mp3":
audio_codec = "libmp3lame"
elif self.output_format == "wav":
audio_codec = "pcm_s16le"
else:
audio_codec = "libmp3lame" # default
cmd = [
"ffmpeg",
"-i",
str(input_file),
"-vn", # no video
"-acodec",
audio_codec,
"-ab",
self.bitrate,
"-ar",
str(self.samplerate),
"-y" if self.overwrite else "-n",
"-loglevel",
"error",
str(output_path),
]
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
)
if duration > 0 and progress_callback:
t = threading.Thread(
target=self.monitor_progress,
args=(process, duration, progress_callback, input_file),
)
t.daemon = True
t.start()
stdout, stderr = process.communicate()
if process.returncode == 0:
logger.info(f"Converted: {input_file} -> {output_path}")
if self.delete_original:
os.remove(input_file)
return True
else:
logger.error(f"Error converting {input_file}: {stderr}")
return False
def monitor_progress(self, process, duration, callback, filename):
start = time.time()
while process.poll() is None:
elapsed = time.time() - start
progress = min(100, (elapsed / duration) * 100)
callback(filename, progress)
time.sleep(0.5)
callback(filename, 100)
# Wrapper for tests
def convert_to_audio(
input_file,
output_file=None,
bitrate="192k",
samplerate=44100,
overwrite=False,
output_format="mp3",
):
converter = VideoToAudioConverter(
bitrate=bitrate,
samplerate=samplerate,
overwrite=overwrite,
output_format=output_format,
)
return converter.convert_video_to_audio(input_file)
# === MAIN EXECUTION ===
if __name__ == "__main__":
print("🎵 VIDEO TO AUDIO CONVERTER 🎵")
print("=" * 50)
# Verifica FFmpeg
converter = VideoToAudioConverter()
if not converter.check_ffmpeg_installed():
print("❌ ERRORE: FFmpeg non trovato!")
print("Installa FFmpeg da: https://ffmpeg.org/download.html")
sys.exit(1)
# Cerca file video supportati nella cartella
video_files = []
for fmt in converter.supported_input_formats:
video_files.extend(Path(".").glob(f"*{fmt}"))
video_files.extend(Path(".").glob(f"*{fmt.upper()}"))
if not video_files:
print("❌ Nessun file video supportato trovato!")
print("Formati supportati:")
for fmt in converter.supported_input_formats:
print(f" - {fmt}")
print(f"\nInserisci un file video in questa cartella:")
print(f" {os.getcwd()}")
sys.exit(1)
# Seleziona file
if len(video_files) == 1:
video_file = str(video_files[0])
print(f"📹 Trovato file: {video_file}")
else:
print("📹 File video trovati:")
for i, file in enumerate(video_files, 1):
print(f" {i}. {file.name}")
try:
scelta = int(
input("\nScegli il numero del file da convertire: ")) - 1
video_file = str(video_files[scelta])
except (ValueError, IndexError):
print("❌ Scelta non valida!")
sys.exit(1)
# Seleziona formato output
print("\n🎵 Formati output disponibili:")
print(" 1. MP3 (consigliato)")
print(" 2. WAV (qualità superiore)")
try:
formato_scelta = input(
"Scegli il formato output (1-2, default: 1): ").strip()
if formato_scelta == "2":
output_format = "wav"
bitrate = "320k" # Migliore qualità per WAV
else:
output_format = "mp3"
bitrate = "192k"
except:
output_format = "mp3"
bitrate = "192k"
# Configura il convertitore
converter = VideoToAudioConverter(
output_dir="converted_audio",
overwrite=True,
bitrate=bitrate,
output_format=output_format,
)
# Funzione per mostrare il progresso
def show_progress(filename, progress):
if progress == 100:
print(f"✅ {Path(filename).name} - Completato!")
elif progress % 25 == 0:
print(f"🔄 {Path(filename).name} - {progress:.1f}%")
# Converti!
print(f"\n🎬 Convertendo {
Path(video_file).name} -> {output_format.upper()}...")
print("⏳ Questo potrebbe richiedere alcuni minuti...")
success = converter.convert_video_to_audio(video_file, show_progress)
if success:
print("\n🎉 CONVERSIONE COMPLETATA!")
output_filename = Path(video_file).stem + f".{output_format}"
print(f"📁 File salvato in: converted_audio/{output_filename}")
else:
print("\n❌ CONVERSIONE FALLITA!")
print("📄 Controlla il file 'conversion.log' per dettagli sull'errore")