From 888b452c9a99e0cb1aa59cee025b4147f8e345f4 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Tue, 8 Jun 2021 22:09:54 +0800 Subject: [PATCH] Update 'split-album' script and sway config --- bin/split-album | 52 +++++++++++++++++++++++++++---------------------- emacs/config.el | 52 +------------------------------------------------ emacs/init.el | 1 - sway/config | 8 +++----- 4 files changed, 33 insertions(+), 80 deletions(-) diff --git a/bin/split-album b/bin/split-album index 6616784..cb1fa81 100755 --- a/bin/split-album +++ b/bin/split-album @@ -1,12 +1,5 @@ -#!/usr/bin/env oil - -# The following are the dependencies for this script. -# -# * Oil shell v0.8.8 -# * coreutils v8.32 -# * file 5-32 -# * ffmpeg v4.3.1 -# * ripgrep v12.1.1 +#! /usr/bin/env nix-shell +#! nix-shell -i oil -p coreutils moreutils ffmpeg gnused ripgrep shopt --set strict:all @@ -23,6 +16,7 @@ Options: -d, --date [date] Set the publication date of the album. -a, --author [author] Set the author of the album. --json Print the JSON data for use with other applications. + --skip Skip the extraction part, useful for printing data with '--json' or testing the timestamp file. --strict Set to be strict, exiting when an invalid timestamp is encountered. Environment variables: @@ -100,6 +94,7 @@ var pub_date = '' var prints_json = false var strict_mode = false +var skip = false while test $len(ARGV) -gt 0 { case $[ARGV[0]] { @@ -135,6 +130,10 @@ while test $len(ARGV) -gt 0 { setvar strict_mode = true shift ;; + --skip) + setvar skip = true + shift + ;; --json) setvar prints_json = true shift @@ -151,9 +150,9 @@ proc warnf(msg, @args) { >&2 printf "${msg}\\n" @args } -proc errorf(msg, exit_code = 1, @args) { +proc errorf(msg, @args) { >&2 printf "${msg}\\n" @args - exit ${exit_code} + exit 1 } proc prompt(msg, :out, prefix = ">> ") { @@ -190,20 +189,20 @@ case $(file --mime-type --brief $timestamp_file) { setvar output_data['chapters'] = chapters ;; - # Also cleans up the timestamp file with comments and empty lines. + # Also cleans up the timestamp file with comments (i.e., lines starting with '#') and empty lines allowing for more commenting options. # I just want to improve the timestamp format (a little bit). "text/plain") sed --regexp-extended --expression '/^\s*$/d' --expression '/^#/d' $timestamp_file | while read --line { var chapter = {} setvar chapter['title'] = $(write -- $_line | cut -d' ' -f2-) setvar chapter['timestamp'] = $(write -- $_line | cut -d' ' -f1) - + write -- ${chapter['timestamp']} | rg --quiet $timestamp_regex || { warnf "'%s' %s is not a valid timestamp" ${chapter['timestamp']} ${chapter['title']} setvar has_error = true continue } - + _ output_data['chapters'].append(chapter) } ;; @@ -212,25 +211,32 @@ case $(file --mime-type --brief $timestamp_file) { if (strict_mode and has_error) { exit 1 } const title_slug = $(kebab-case $album) -setvar output_data['directory'] = title_slug +setvar output_data['directory'] = $(realpath --canonicalize-missing $title_slug) mkdir -p $title_slug +# Rather than sequentially segmenting the audio, we'll extract the starting and ending timestamps of each segment then feed it to a job queue that can execute jobs in parallel. +# Take note we don't have the ending timestamp of each segment so we need a way to look back into items. const chapter_len = len(output_data['chapters']) +var job_queue = %() for index in @(seq $[chapter_len]) { - setvar chapter = output_data['chapters'][Int(index) - 1] + var index = Int(index) + var chapter = output_data['chapters'][index - 1] var start = chapter['timestamp'] - var end = output_data['chapters'][Int(index)]['timestamp'] if Int(index) != chapter_len else null + var end = output_data['chapters'][index]['timestamp'] if index != chapter_len else null var filename = $(printf "%.2d-%s.%s" $index $(kebab-case ${chapter['title']}) $EXTENSION) - setvar output_data['chapters'][Int(index) - 1]['file'] = filename + setvar output_data['chapters'][index - 1]['file'] = filename - warnf "[%d/%d] %s" ${index} ${chapter_len} ${chapter['title']} - if (Int(index) != chapter_len) { - ffmpeg -loglevel quiet -nostdin -i $audio_file -ss $start -to $end "${title_slug}/${filename}" - } else { - ffmpeg -loglevel quiet -nostdin -i $audio_file -ss $start "${title_slug}/${filename}" + if (start > end and end is not null) { + warnf '%s (start) is ahead compared to %s (end)' $start $end + setvar has_error = true } + + push :job_queue "ffmpeg -loglevel quiet -nostdin -i '${audio_file}' -ss ${start} $['-to ' + end if index != chapter_len else ''] ${title_slug}/${filename}" } +if (has_error) { exit 1 } + +if (not skip) { parallel -- @job_queue } if (prints_json) { json write :output_data } diff --git a/emacs/config.el b/emacs/config.el index 2fe3537..e1e4c49 100755 --- a/emacs/config.el +++ b/emacs/config.el @@ -71,55 +71,8 @@ ("c" "org-protocol-capture" entry (file ,(f-join org-directory "inbox.org")) "* TODO [[%:link][%:description]]\n%x" - :immediate-finish t)) + :immediate-finish t)))) - org-roam-capture-templates `( - ("p" "permanent" plain "%?" - :if-new - (file+head "%<%Y-%m-%d-%H-%M-%S>.org" - "#+title: ${title} -#+date: \"%<%Y-%m-%d %T %:z>\" -#+date_modified: \"%<%Y-%m-%d %T %:z>\" -#+language: en") - :unnarrowed t) - - ("c" "cards" plain "%?" - :if-new - (file+head ,(f-join +anki-cards-directory-name "%<%Y>.org") "#+title: Anki: ${title} -#+date: \"%<%Y-%m-%d %T %:z>\" -#+date_modified: \"%<%Y-%m-%d %T %:z>\" -#+language: en -#+property: anki_deck ${title}") - :unnarrowed t) - - ("C" "challenges" plain "%?" - :if-new - (file+head ,(f-join +wiki-directory my/wiki-exercises-directory "${slug}.org") "#+title: ${title} -#+date: \"%<%Y-%m-%d %T %:z>\" -#+date_modified: \"%<%Y-%m-%d %T %:z>\" -#+language: en") - :unnarrowed t) - - ("l" "literature" plain "%?" - :if-new - (file+head ,(f-join "literature" "%<%Y-%m-%d-%H-%M-%S>.org") "#+title: ${title} -#+date: \"%<%Y-%m-%d %T %:z>\" -#+date_modified: \"%<%Y-%m-%d %T %:z>\" -#+language: en") - :unnarrowed t) - - ("d" "dailies" entry "* %?" - :if-new - (file+head ,(expand-file-name "%<%Y-%m-%d>.org" org-roam-dailies-directory) "#+title: %<%Y-%m-%d>\n")) - - ("s" "structured" plain "%?" - :if-new - (file+head ,(f-join +structured-notes-directory-name "${slug}.org") "#+title: ${title}") - :unnarrowed t)) - - org-roam-dailies-capture-templates `(("d" "default" entry "* %?" - :if-new - (file+head ,(expand-file-name "%<%Y-%m-%d>.org" org-roam-dailies-directory) "#+title: %<%Y-%m-%d>\n"))))) ;; Custom keybindings (map! @@ -138,9 +91,6 @@ ;; Automate updating timestamps on save. (add-hook! 'before-save-hook 'time-stamp) -;; Add a capture hook. -(add-hook! 'org-roam-capture-new-node-hook 'org-id-get-create) - ;; Load a custom configuration for muh wiki. (load-file (f-join +wiki-directory "config.el")) diff --git a/emacs/init.el b/emacs/init.el index 625a3af..b0e9e29 100755 --- a/emacs/init.el +++ b/emacs/init.el @@ -186,5 +186,4 @@ :tools (wiki +anki - +dendron +biblio)) diff --git a/sway/config b/sway/config index 742d70e..3146b6a 100644 --- a/sway/config +++ b/sway/config @@ -74,6 +74,7 @@ default_border pixel # Start your launcher bindsym $mod+d exec $menu + bindsym $mod+shift+d exec wofi --show drun # Volume bindsym XF86AudioRaiseVolume exec pactl set-sink-volume @DEFAULT_SINK@ +5% @@ -159,13 +160,10 @@ default_border pixel # You can "split" the current object of your focus with # $mod+b or $mod+v, for horizontal and vertical splits # respectively. - bindsym $mod+b splith - bindsym $mod+v splitv + bindsym $mod+c split toggle # Switch the current container between different layout styles - bindsym $mod+s layout stacking - bindsym $mod+w layout tabbed - bindsym $mod+e layout toggle split + bindsym $mod+s layout toggle stacking split tabbed bindsym $mod+f fullscreen bindsym $mod+Shift+f floating toggle