Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb20a034e6 | ||
|
|
cdd380d62f |
No files matched your search
+1
-1
@@ -5,7 +5,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
VERSION = "v1.9.0"
|
VERSION = "v1.9.2"
|
||||||
|
|
||||||
|
|
||||||
APP_NAME = "MusicTools"
|
APP_NAME = "MusicTools"
|
||||||
|
|||||||
+70
-3
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import queue
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import threading
|
import threading
|
||||||
@@ -13,6 +14,10 @@ from core.paths import find_ytdlp, find_ffmpeg_dir, find_ffprobe, subprocess_fla
|
|||||||
|
|
||||||
AUDIO_EXTENSIONS = {".mp3", ".m4a", ".wav", ".flac"}
|
AUDIO_EXTENSIONS = {".mp3", ".m4a", ".wav", ".flac"}
|
||||||
|
|
||||||
|
# Timeout massimo per singolo download yt-dlp (secondi). Watchdog kill.
|
||||||
|
# Serve a evitare hang su video geo-restricted, YouTube throttle o rete lenta.
|
||||||
|
_DOWNLOAD_TIMEOUT_SEC = 300
|
||||||
|
|
||||||
# Flag globale per interruzione
|
# Flag globale per interruzione
|
||||||
_stop_event = threading.Event()
|
_stop_event = threading.Event()
|
||||||
_current_process: Optional[subprocess.Popen] = None
|
_current_process: Optional[subprocess.Popen] = None
|
||||||
@@ -288,17 +293,79 @@ def upgrade_folder(
|
|||||||
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
|
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
|
||||||
**subprocess_flags(),
|
**subprocess_flags(),
|
||||||
)
|
)
|
||||||
|
proc = _current_process
|
||||||
|
|
||||||
for line in _current_process.stdout:
|
# Watchdog: kill del subprocess se supera _DOWNLOAD_TIMEOUT_SEC.
|
||||||
|
# Previene hang indefinito su video problematici (geo-block,
|
||||||
|
# YouTube throttle, rete lenta).
|
||||||
|
def _watchdog_kill():
|
||||||
|
try:
|
||||||
|
if proc.poll() is None:
|
||||||
|
proc.kill() # SIGKILL — SIGTERM può lasciare ffmpeg orfano che tiene aperto il pipe
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
watchdog = threading.Timer(_DOWNLOAD_TIMEOUT_SEC, _watchdog_kill)
|
||||||
|
watchdog.daemon = True
|
||||||
|
watchdog.start()
|
||||||
|
|
||||||
|
# Lettore stdout in thread separato + queue: il main loop polla
|
||||||
|
# con timeout invece di bloccare su `for line in proc.stdout`.
|
||||||
|
# Cosi is_stopped() e proc.poll() vengono controllati periodicamente
|
||||||
|
# -> il bottone Stop risponde in <1s anche se il subprocess ha
|
||||||
|
# figli orfani (ffmpeg) che tengono aperto il pipe.
|
||||||
|
output_q: queue.Queue = queue.Queue()
|
||||||
|
|
||||||
|
def _reader():
|
||||||
|
try:
|
||||||
|
for line in proc.stdout:
|
||||||
|
output_q.put(line)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
output_q.put(None) # sentinel: pipe closed
|
||||||
|
|
||||||
|
reader = threading.Thread(target=_reader, daemon=True)
|
||||||
|
reader.start()
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
if is_stopped():
|
if is_stopped():
|
||||||
_current_process.terminate()
|
try:
|
||||||
|
proc.kill()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return
|
return
|
||||||
|
try:
|
||||||
|
line = output_q.get(timeout=0.5)
|
||||||
|
except queue.Empty:
|
||||||
|
# Se il subprocess e' morto e il pipe non produce piu' output
|
||||||
|
# (child orfano), esci comunque.
|
||||||
|
if proc.poll() is not None and output_q.empty():
|
||||||
|
# Aspetta ancora un attimo per drenare
|
||||||
|
try:
|
||||||
|
line = output_q.get(timeout=1.0)
|
||||||
|
except queue.Empty:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
if line is None:
|
||||||
|
break
|
||||||
pct_match = re.search(r"(\d+(?:\.\d+)?)%", line)
|
pct_match = re.search(r"(\d+(?:\.\d+)?)%", line)
|
||||||
if pct_match and progress_callback:
|
if pct_match and progress_callback:
|
||||||
pct = int(float(pct_match.group(1)))
|
pct = int(float(pct_match.group(1)))
|
||||||
progress_callback(processed, total, filepath.name, "downloading", current_kbps, pct)
|
progress_callback(processed, total, filepath.name, "downloading", current_kbps, pct)
|
||||||
|
|
||||||
_current_process.wait()
|
try:
|
||||||
|
proc.wait(timeout=10)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
try:
|
||||||
|
proc.kill()
|
||||||
|
proc.wait(timeout=5)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
watchdog.cancel()
|
||||||
|
|
||||||
with _process_lock:
|
with _process_lock:
|
||||||
_current_process = None
|
_current_process = None
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user