|
|
|
@ -9,21 +9,105 @@ import math
|
|
|
|
|
import json
|
|
|
|
|
import argparse
|
|
|
|
|
import ffmpeg
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
import pydbus as dbus
|
|
|
|
|
|
|
|
|
|
import openpyxl
|
|
|
|
|
from openpyxl.styles import PatternFill
|
|
|
|
|
from openpyxl.formatting.rule import CellIsRule
|
|
|
|
|
|
|
|
|
|
date = datetime.now().strftime("%Y-%m-%d_%Hh%M")
|
|
|
|
|
|
|
|
|
|
def inhibit_arret(application, raison):
|
|
|
|
|
inhibiteurs = {}
|
|
|
|
|
|
|
|
|
|
# Inhibition arrêt et veille via login1 (SystemBus)
|
|
|
|
|
system_bus = dbus.SystemBus()
|
|
|
|
|
login1 = system_bus.get("org.freedesktop.login1", "/org/freedesktop/login1")
|
|
|
|
|
|
|
|
|
|
inhibiteurs["shutdown"] = login1.Inhibit(
|
|
|
|
|
"shutdown",
|
|
|
|
|
application,
|
|
|
|
|
raison,
|
|
|
|
|
"block"
|
|
|
|
|
)
|
|
|
|
|
inhibiteurs["sleep"] = login1.Inhibit(
|
|
|
|
|
"sleep",
|
|
|
|
|
application,
|
|
|
|
|
raison,
|
|
|
|
|
"block"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Inhibition déconnexion via gnome-session (SessionBus)
|
|
|
|
|
session_bus = dbus.SessionBus()
|
|
|
|
|
session_manager = session_bus.get("org.gnome.SessionManager", "/org/gnome/SessionManager")
|
|
|
|
|
|
|
|
|
|
# flags:
|
|
|
|
|
# 0 = logout only
|
|
|
|
|
# 1 = user switch
|
|
|
|
|
# 2 = suspend
|
|
|
|
|
# 4 = shutdown
|
|
|
|
|
# 8 = idle (screensaver, etc)
|
|
|
|
|
flags = 0 | 1 | 2 | 4 | 8
|
|
|
|
|
inhibiteurs["session"] = session_manager.Inhibit(
|
|
|
|
|
application,
|
|
|
|
|
0,
|
|
|
|
|
raison,
|
|
|
|
|
flags
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return inhibiteurs
|
|
|
|
|
|
|
|
|
|
def lever_inhibit_arret(inhibiteurs):
|
|
|
|
|
# Fermer le descripteur de fichier pour lever l'inhibition
|
|
|
|
|
for cle, inhibiteur in inhibiteurs.items():
|
|
|
|
|
if cle in ["shutdown", "sleep"] and hasattr(inhibiteur, "close"):
|
|
|
|
|
inhibiteur.close()
|
|
|
|
|
elif cle == "session":
|
|
|
|
|
# Connexion au bus session
|
|
|
|
|
session_bus = dbus.SessionBus()
|
|
|
|
|
# Obtenir l'interface SessionManager de gnome
|
|
|
|
|
session_manager = session_bus.get("org.gnome.SessionManager", "/org/gnome/SessionManager")
|
|
|
|
|
session_manager.Uninhibit(inhibiteur)
|
|
|
|
|
|
|
|
|
|
def relancer_avec_systemd():
|
|
|
|
|
# Constitue la commande avec systemd-run
|
|
|
|
|
cmd = [
|
|
|
|
|
"systemd-run",
|
|
|
|
|
"--unit=handbrake_recursive",
|
|
|
|
|
"--user",
|
|
|
|
|
"--description=Encodage vidéo",
|
|
|
|
|
f'--working-directory={os.getcwd()}',
|
|
|
|
|
"--",
|
|
|
|
|
sys.argv[0]
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# Ajoute les arguments sauf --service et --pager (journalctl utilise déjà un pager)
|
|
|
|
|
cmd += [arg for arg in sys.argv[1:] if arg not in ("--service", "--pager")]
|
|
|
|
|
|
|
|
|
|
print(f"Lancement du script via systemd:\n{' '.join(cmd)}")
|
|
|
|
|
print(f"Pour suivre l'exécution du programme:\n journalctl --user -f -u handbrake_recursive")
|
|
|
|
|
|
|
|
|
|
# Remplace le processus courant
|
|
|
|
|
os.execvp(cmd[0], cmd)
|
|
|
|
|
|
|
|
|
|
# Définir la largeur de chaque colonne à environ 3 cm (approximativement 10.5 unités)
|
|
|
|
|
def set_column_width(ws, column_range, width=15):
|
|
|
|
|
for col in column_range:
|
|
|
|
|
ws.column_dimensions[col].width = width
|
|
|
|
|
|
|
|
|
|
def export_xlsx(lignes, fichier_sortie="résumé.xlsx"):
|
|
|
|
|
# Créer un nouveau classeur et sélectionner la feuille active
|
|
|
|
|
wb = openpyxl.Workbook()
|
|
|
|
|
ws = wb.active
|
|
|
|
|
ws.title = "Résumé"
|
|
|
|
|
# Charger le classeur existant ou en créer un nouveau
|
|
|
|
|
if os.path.exists(fichier_sortie):
|
|
|
|
|
wb = openpyxl.load_workbook(fichier_sortie)
|
|
|
|
|
else:
|
|
|
|
|
wb = openpyxl.Workbook()
|
|
|
|
|
# Supprimer la feuille par défaut si on ajoute une nouvelle
|
|
|
|
|
default_sheet = wb.active
|
|
|
|
|
wb.remove(default_sheet)
|
|
|
|
|
|
|
|
|
|
# Créer une nouvelle feuille
|
|
|
|
|
ws = wb.create_sheet(title=date)
|
|
|
|
|
|
|
|
|
|
# En-têtes
|
|
|
|
|
headers = ["Fichier", "Codec", "Résolution", "Action", "Durée", "Taille SRC", "Bitrate SRC",
|
|
|
|
@ -355,8 +439,13 @@ def main():
|
|
|
|
|
parser.add_argument('-o', '--output_directory', required=True, help='Dossier de sortie pour les fichiers traités')
|
|
|
|
|
parser.add_argument('--dry-run', action='store_true', help='Simule les actions sans modifier les fichiers')
|
|
|
|
|
parser.add_argument('--pager', action='store_true', help='Utilise less pour paginer le résumé final')
|
|
|
|
|
parser.add_argument("--service", action="store_true", help="Lancer via systemd-run")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
if args.service:
|
|
|
|
|
if "INVOCATION_ID" not in os.environ:
|
|
|
|
|
relancer_avec_systemd()
|
|
|
|
|
|
|
|
|
|
# Récupération des arguments
|
|
|
|
|
preset_file = args.preset
|
|
|
|
|
input_directory = args.input_directory
|
|
|
|
@ -380,6 +469,8 @@ def main():
|
|
|
|
|
# Liste pour enregistrer le résumé
|
|
|
|
|
lignes = []
|
|
|
|
|
|
|
|
|
|
inhibiteurs = inhibit_arret( "HandBrake Script", "Encodage vidéo en cours")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Traitement de chaque fichier vidéo
|
|
|
|
|
for video_file in video_files:
|
|
|
|
@ -458,11 +549,14 @@ def main():
|
|
|
|
|
"" # Résultat
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
# Affichage du résumé
|
|
|
|
|
afficher_tableau(lignes, use_pager=use_pager)
|
|
|
|
|
xlsx_file = "résumé.xlsx"
|
|
|
|
|
export_xlsx(lignes, fichier_sortie=xlsx_file)
|
|
|
|
|
print(f"📝 Classeur généré : {xlsx_file}")
|
|
|
|
|
|
|
|
|
|
lever_inhibit_arret(inhibiteurs)
|
|
|
|
|
|
|
|
|
|
# Affichage du résumé
|
|
|
|
|
afficher_tableau(lignes, use_pager=use_pager)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|
|
|
|
|