#!/usr/bin/gjs
const
Gtk =
imports
.gi.Gtk;
const
GLib =
imports
.gi.GLib;
const
Gio =
imports
.gi.Gio;
const
Lang =
imports
.lang;
const
System =
imports
.system;
const
NumEntry =
new
Lang.Class({
Name:
'NumEntry'
,
Extends: Gtk.Entry,
_init:
function
() {
this
.parent({
xalign: 1.0
});
let
buf =
this
.get_buffer();
buf.connect(
"inserted-text"
, Lang.bind(
this
,
function
(buf, position, chars, n_chars) {
if
(isNaN(chars))
buf.delete_text(position, n_chars);
}));
}
});
const
FFCut =
new
Lang.Class({
Name:
'FFCut'
,
Extends: Gtk.ApplicationWindow,
_init:
function
(app, basename) {
this
.parent({
application: app,
title: basename
});
let
texts = [
"Start"
,
":"
,
"Stop"
,
":"
];
let
labels = [];
this
.entries = [];
for
(
let
i=0; i<4; i++) {
labels[i] =
new
Gtk.Label({label: texts[i]});
this
.entries[i] =
new
NumEntry();
}
let
grid =
new
Gtk.Grid();
grid.attach(labels[0], 0, 0, 1, 1);
grid.attach(labels[1], 2, 0, 1, 1);
grid.attach(labels[2], 0, 1, 1, 1);
grid.attach(labels[3], 2, 1, 1, 1);
grid.attach(
this
.entries[0], 1, 0, 1, 1);
grid.attach(
this
.entries[1], 3, 0, 1, 1);
grid.attach(
this
.entries[2], 1, 1, 1, 1);
grid.attach(
this
.entries[3], 3, 1, 1, 1);
let
button =
new
Gtk.Button({label:
"Cut"
});
button.connect(
"clicked"
, Lang.bind(
this
,
function
() {
let
ss1 = Number(
this
.entries[0].get_text()) * 60 + Number(
this
.entries[1].get_text());
let
ss2 = Number(
this
.entries[2].get_text()) * 60 + Number(
this
.entries[3].get_text()) - ss1;
let
cmd =
"ffmpeg -ss "
+ ss1 +
" -i "
+
this
.title +
" -t "
+ ss2 +
" -vcodec copy -acodec copy out_"
+
this
.title;
GLib.spawn_command_line_async(cmd);
}));
let
vbox =
new
Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 5
});
vbox.pack_start(grid,
true
,
true
, 0);
vbox.pack_start(button,
true
,
true
, 0);
this
.add(vbox);
this
.show_all();
}
});
const
FFApp =
new
Lang.Class({
Name:
'FFApp'
,
Extends: Gtk.Application,
_init:
function
() {
this
.parent({
application_id:
'org.sasakima.ffcut'
,
flags: Gio.ApplicationFlags.HANDLES_OPEN
});
},
vfunc_open:
function
(files, hint) {
let
basename = files[0].get_basename();
let
w =
new
FFCut(
this
, basename);
},
vfunc_activate:
function
() {
print
(
"Usage: ffcut FILENAME"
);
}
});
let
argv = [System.programInvocationName];
ARGV.forEach(
function
(element) {
if
(element.indexOf(
"//"
) == -1) {
argv.push(decodeURIComponent(escape(element)));
}
else
{
argv.push(element);
}
});
let
application =
new
FFApp();
application.run(argv);