Restructure app and move into files

This commit is contained in:
Jeena 2025-06-01 12:18:58 +09:00
parent 32a4e78d1b
commit 86bf8750de
13 changed files with 205 additions and 186 deletions

28
src/app.py Normal file
View file

@ -0,0 +1,28 @@
import sys
import gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Adw, Gio
from ui import RecoderWindow
Adw.init()
class RecoderApp(Adw.Application):
def __init__(self):
super().__init__(application_id="net.jeena.recoder",
flags=Gio.ApplicationFlags.FLAGS_NONE)
self.window = None
def do_activate(self):
if not self.window:
self.window = RecoderWindow(application=self)
self.window.present()
def main():
app = RecoderApp()
return app.run(sys.argv)
if __name__ == "__main__":
sys.exit(main())

16
src/config.py Normal file
View file

@ -0,0 +1,16 @@
import json
from pathlib import Path
from gi.repository import GLib
CONFIG_PATH = Path(GLib.get_user_config_dir()) / "recoder" / "config.json"
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
def save_config(config):
with open(CONFIG_PATH, "w") as f:
json.dump(config, f)
def load_config():
if CONFIG_PATH.exists():
with open(CONFIG_PATH) as f:
return json.load(f)
return {}

13
src/models.py Normal file
View file

@ -0,0 +1,13 @@
from gi.repository import GObject, Gio
class FileStatus(GObject.GEnum):
__gtype_name__ = 'FileStatus'
ERROR = 0
WAITING = 1
PROCESSING = 2
DONE = 3
class FileItem(GObject.GObject):
file = GObject.Property(type=Gio.File)
progress = GObject.Property(type=int, minimum=0, maximum=100, default=0)
status = GObject.Property(type=FileStatus, default=FileStatus.WAITING)

57
src/transcoder_worker.py Normal file
View file

@ -0,0 +1,57 @@
import os
import threading
from gi.repository import GLib
import subprocess
def transcode_file(input_path, output_dir):
os.makedirs(output_dir, exist_ok=True)
basename = os.path.basename(input_path)
output_path = os.path.join(output_dir, basename)
cmd = [
"ffmpeg", "-y", "-i", input_path,
"-c:v", "libx264", "-preset", "fast",
"-c:a", "aac", output_path
]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return result.returncode == 0, output_path
class TranscoderWorker:
def __init__(self, files, progress_callback=None, done_callback=None):
self.files = files
self.progress_callback = progress_callback
self.done_callback = done_callback
self.is_processing = False
def start(self):
if self.is_processing:
return
self.is_processing = True
thread = threading.Thread(target=self._process_files)
thread.daemon = True
thread.start()
def _process_files(self):
total = len(self.files)
for idx, filepath in enumerate(self.files, start=1):
basename = os.path.basename(filepath)
self._update_progress(f"Processing {basename} ({idx}/{total})...", idx / total)
success, output_path = transcode_file(filepath, os.path.join(os.path.dirname(filepath), "transcoded"))
if not success:
self._update_progress(f"Error transcoding {basename}", idx / total)
else:
self._update_progress(f"Finished {basename}", idx / total)
self.is_processing = False
self._update_progress("All done!", 1.0)
self._notify_done()
def _update_progress(self, text, fraction):
if self.progress_callback:
GLib.idle_add(self.progress_callback, text, fraction)
def _notify_done(self):
if self.done_callback:
GLib.idle_add(self.done_callback)

164
src/ui.py Normal file
View file

@ -0,0 +1,164 @@
import os
import gi
import shutil
import subprocess
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw, Gio, Gdk, GLib
from functools import partial
from config import load_config, save_config
from transcoder_worker import TranscoderWorker
class RecoderWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title("Recoder")
self.set_default_size(700, 400)
self.config = load_config()
self.current_dir = self.config.get("last_directory", str(os.path.expanduser("~")))
self.files_to_process = []
self.transcoder = None
# UI setup
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.header_bar = Adw.HeaderBar()
self.toolbar_view = Adw.ToolbarView()
self.toolbar_view.add_top_bar(self.header_bar)
self.toolbar_view.set_content(self.vbox)
self.set_content(self.toolbar_view)
self.textview = Gtk.TextView()
self.textview.set_editable(False)
self.textbuffer = self.textview.get_buffer()
self.drop_target = Gtk.DropTarget.new(Gio.File, Gdk.DragAction.COPY)
self.drop_target.connect("enter", self.on_drop_enter)
self.drop_target.connect("leave", self.on_drop_leave)
self.drop_target.connect("drop", self.on_drop)
self.textview.add_controller(self.drop_target)
# CSS
self._setup_css()
self.drop_hint = Gtk.Label(label="📂 Drop files here to get started")
self.drop_hint.add_css_class("dim-label")
self._setup_dim_label_css()
overlay = Gtk.Overlay()
overlay.set_child(self.textview)
overlay.add_overlay(self.drop_hint)
self.drop_hint.set_halign(Gtk.Align.CENTER)
self.drop_hint.set_valign(Gtk.Align.CENTER)
self.scrolled = Gtk.ScrolledWindow()
self.scrolled.set_child(overlay)
self.vbox.append(self.scrolled)
self.scrolled.set_vexpand(True)
self.scrolled.set_hexpand(True)
self.progress = Gtk.ProgressBar()
self.vbox.append(self.progress)
self.progress.set_hexpand(True)
def _setup_css(self):
css = b"""
textview.drop-highlight {
background-color: @theme_selected_bg_color;
border: 2px solid @theme_selected_fg_color;
}
"""
style_provider = Gtk.CssProvider()
style_provider.load_from_data(css)
Gtk.StyleContext.add_provider_for_display(
Gdk.Display.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION,
)
def _setup_dim_label_css(self):
css = b"""
.dim-label {
font-size: 16px;
color: @theme_unfocused_fg_color;
}
"""
style_provider = Gtk.CssProvider()
style_provider.load_from_data(css)
Gtk.StyleContext.add_provider_for_display(
Gdk.Display.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION,
)
def on_drop_enter(self, drop_target, x, y):
self.textview.add_css_class("drop-highlight")
return True
def on_drop_leave(self, drop_target):
self.textview.remove_css_class("drop-highlight")
return True
def on_drop(self, drop_target, value, x, y):
GLib.idle_add(partial(self.process_drop_value, value))
return value
def process_drop_value(self, value):
if isinstance(value, Gio.File):
uris = [value]
elif isinstance(value, list):
uris = value
else:
return False
paths = []
for file in uris:
path = file.get_path()
if os.path.isdir(path):
for entry in os.scandir(path):
if entry.is_file() and entry.name.lower().endswith((".mp4", ".mov", ".mkv", ".avi")):
paths.append(entry.path)
else:
paths.append(path)
if not paths:
return False
self.textview.remove_css_class("drop-highlight")
self.drop_hint.set_visible(False)
self.files_to_process = paths
self.textbuffer.set_text("\n".join(os.path.basename(p) for p in paths) + "\n")
self.start_transcoding()
return False
def start_transcoding(self):
if self.transcoder and self.transcoder.is_processing:
return
self.progress.set_fraction(0.0)
self.progress.set_text("Starting transcoding...")
self.transcoder = TranscoderWorker(
self.files_to_process,
progress_callback=self.update_progress,
done_callback=self.notify_done,
)
self.transcoder.start()
def update_progress(self, text, fraction):
self.progress.set_show_text(True)
self.progress.set_text(text)
self.progress.set_fraction(fraction)
def notify_done(self):
self.progress.set_show_text(True)
self.progress.set_text("Transcoding Complete!")
self.play_complete_sound()
def play_complete_sound(self):
if shutil.which("canberra-gtk-play"):
subprocess.Popen(["canberra-gtk-play", "--id", "complete"])
else:
print("canberra-gtk-play not found.")