This commit is contained in:
SqrtMinusOne 2022-10-13 20:02:40 +00:00
parent 4dcc2871d5
commit 61a78bea72
3 changed files with 79 additions and 62 deletions

View file

@ -640,6 +640,8 @@ Emacs is also particularly good at writing Lisp code, e.g. Clojure, Common Lisp,
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>(<span style="color:#19177c">my-leader-def</span>
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;fx&#34;</span> <span style="color:#19177c">&#39;xref-find-apropos</span>)
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">use-package</span> <span style="color:#19177c">xref</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000">:straight</span> (<span style="color:#008000">:type</span> <span style="color:#19177c">built-in</span>))
</span></span></code></pre></div><h5 id="folding">Folding</h5>
<p>There are multiple ways to fold text in Emacs.</p>
<p>The most versatile is the built-in <code>hs-minor-mode</code>, which seems to work out of the box for Lisps, C-like languages, and Python. <code>outline-minor-mode</code> works for org-mode, LaTeX and the like. There is a 3rd-party solution <a href="https://github.com/elp-revive/origami.el">origami.el</a>, which I found to be somewhat less stable.</p>
@ -3525,7 +3527,7 @@ Emacs is also particularly good at writing Lisp code, e.g. Clojure, Common Lisp,
</span></span><span style="display:flex;"><span> <span style="color:#666">,@</span><span style="color:#19177c">project-files</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">setq</span> <span style="color:#19177c">org-refile-targets</span>
</span></span><span style="display:flex;"><span> <span style="color:#666">`</span>(<span style="color:#666">,@</span>(<span style="color:#00f">mapcar</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">lambda</span> (<span style="color:#19177c">f</span>) <span style="color:#666">`</span>(<span style="color:#666">,</span><span style="color:#19177c">f</span> <span style="color:#666">.</span> (<span style="color:#008000">:level</span> <span style="color:#666">.</span> <span style="color:#666">2</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">lambda</span> (<span style="color:#19177c">f</span>) <span style="color:#666">`</span>(<span style="color:#666">,</span><span style="color:#19177c">f</span> <span style="color:#666">.</span> (<span style="color:#008000">:maxlevel</span> <span style="color:#666">.</span> <span style="color:#666">2</span>)))
</span></span><span style="display:flex;"><span> <span style="color:#19177c">project-files</span>)
</span></span><span style="display:flex;"><span> <span style="color:#666">,@</span>(<span style="color:#00f">mapcar</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">lambda</span> (<span style="color:#19177c">f</span>) <span style="color:#666">`</span>(<span style="color:#666">,</span><span style="color:#19177c">f</span> <span style="color:#666">.</span> (<span style="color:#008000">:tag</span> <span style="color:#666">.</span> <span style="color:#ba2121">&#34;refile&#34;</span>)))
@ -4186,7 +4188,8 @@ Emacs is also particularly good at writing Lisp code, e.g. Clojure, Common Lisp,
</span></span><span style="display:flex;"><span> (<span style="color:#00f">buffer-file-name</span>))
</span></span><span style="display:flex;"><span> <span style="color:#19177c">name</span> <span style="color:#ba2121">&#34;.csv&#34;</span>)
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;orgtbl-to-csv&#34;</span>)))))
</span></span></code></pre></div><h3 id="ui">UI</h3>
</span></span></code></pre></div><h4 id="copying-records">Copying records</h4>
<h3 id="ui">UI</h3>
<h4 id="off--instant-equations-preview"><span class="org-todo done OFF">OFF</span> (OFF) Instant equations preview</h4>
<p>Instant math previews for org mode.</p>
<p>References:</p>
@ -5717,85 +5720,77 @@ Emacs is also particularly good at writing Lisp code, e.g. Clojure, Common Lisp,
<p>In my experience, finding something in a podcast is particularly troublesome. For example, occasionally I want to refer to some line in the podcast to make an <a href="https://github.com/org-roam/org-roam">org-roam</a> node, e.g. I want to check that I got that part right.</p>
<p>And I have no reasonable way to get there because audio files in themselves don&rsquo;t allow for <a href="https://en.wikipedia.org/wiki/Random_access">random access</a>, i.e. there are no &ldquo;landmarks&rdquo; that point to this or that portion of the file. At least if nothing like a transcript is available.</p>
<p>For obvious reasons, podcasts rarely ship with transcripts. So in this post, I&rsquo;ll be using a speech recognition engine to make up for that. A generated transcript is not quite as good as a manually written one, but for the purpose of finding a fragment of a known podcast, it works well enough.</p>
<p>The general idea is to get the podcast info from <a href="https://github.com/skeeto/elfeed">elfeed</a>, process it with <a href="https://github.com/alphacep/vosk-api">vosk-api</a> and feed it to <a href="https://github.com/sachac/subed">subed</a> to control the playback in <a href="https://mpv.io/">MPV</a>.</p>
<h5 id="vosk-api">Vosk API</h5>
<p>After some search, I found <a href="https://github.com/alphacep/vosk-api">Vosk API</a>, an offline speech recognition toolkit.</p>
<p>I want to make a program that receives an audio file and outputs an <a href="https://en.wikipedia.org/wiki/SubRip">SRT</a> file. Vosk provides bindings to different languages, of which I choose Python because&hellip; reasons.</p>
<p>So, with the help of kindly provided <a href="https://github.com/alphacep/vosk-api/tree/master/python/example">examples</a> of how to use the Python API, the resulting script is <a href="https://github.com/SqrtMinusOne/podcasts-vosk">available here</a>. Except Vosk, the script uses <a href="https://click.palletsprojects.com/en/8.1.x/">click</a> to make a simple CLI, a library aptly called <a href="https://github.com/cdown/srt">srt</a> to make srt files, and <a href="https://ffmpeg.org/">ffmpeg</a>.</p>
<p>Another piece we need is a speech recognition model, some of which you can download <a href="https://alphacephei.com/vosk/models">on their website</a>. I chose a small English model called <code>vosk-model-small-en-us-0.15</code> because all my podcasts are in English and also because larger models are much slower.</p>
<p>Now that we have the script and the model, we need to create a virtual environment. Somehow I couldn&rsquo;t install the <code>vosk</code> package with <a href="https://docs.conda.io/en/latest/">conda</a>, but the Guix version of Python with <code>virtualenv</code> worked just fine:</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>python3 -m virtualenv venv
<p>The general idea is to get the podcast info from <a href="https://github.com/skeeto/elfeed">elfeed</a>, process it with <a href="https://github.com/openai/whisper">OpenAI Whisper</a> and feed it to <a href="https://github.com/sachac/subed">subed</a> to control the playback in <a href="https://mpv.io/">MPV</a>.</p>
<p>Edit <span class="timestamp-wrapper"><span class="timestamp">&lt;2022-10-08 Sat&gt;</span></span>: Changed <a href="https://github.com/alphacep/vosk-api">vosk-api</a> to OpenAI Whisper.</p>
<h5 id="whisper">Whisper</h5>
<p><a href="https://github.com/openai/whisper">OpenAI Whisper</a> is an amazing speech recognition toolkit. It&rsquo;s pretty slow on my PC (compared to <a href="https://github.com/alphacep/vosk-api">vosk-api</a> which I&rsquo;ve been using before), but the quality is so much better so I think it&rsquo;s completely worth it.</p>
<p>All we need to do is install Whisper in a virtual environment:</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>python -m virtualenv venv
</span></span><span style="display:flex;"><span><span style="color:#008000">source</span> venv/bin/activate
</span></span><span style="display:flex;"><span>pip install -r requirements.txt
</span></span></code></pre></div><p>After which the script can be used as follows:</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>python main.py --file-path &lt;path-to-file&gt; --model-path ./model-small --save-path &lt;path-to-subtitles-file&gt;.srt
</span></span><span style="display:flex;"><span>pip install whisper
</span></span></code></pre></div><p>After which the program can be used as follows:</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>whisper &lt;path-to-file&gt; --output-dir &lt;path-to-output-dir&gt;
</span></span></code></pre></div><h5 id="running-it-from-emacs">Running it from Emacs</h5>
<p>The next step is to run the script from Emacs. This is rather straightforward to do with <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Asynchronous-Processes.html">asyncronous processes</a>.</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">defvar</span> <span style="color:#19177c">my/vosk-script-path</span>
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;/home/pavel/Code/system-crafting/podcasts-vosk/&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;Path to the </span><span style="color:#19177c">`podcasts-vosk&#39;</span><span style="color:#ba2121"> script folder.&#34;</span>)
<p>Running the program from Emacs is rather straightforward with <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Asynchronous-Processes.html">asyncronous processes</a>.</p>
<p>I&rsquo;m using an English-language-only model because that&rsquo;s the only language I need at the moment.</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">defvar</span> <span style="color:#19177c">my/whisper-env-path</span>
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;/home/pavel/Code/system-crafting/whisper-test/&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;Path to the folder with </span><span style="color:#19177c">`whisper&#39;</span><span style="color:#ba2121"> environment.&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/invoke-vosk</span> (<span style="color:#19177c">input</span> <span style="color:#19177c">output</span>)
</span></span><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/invoke-whisper</span> (<span style="color:#19177c">input</span> <span style="color:#19177c">output-dir</span>)
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;Extract subtitles from the audio file.
</span></span></span><span style="display:flex;"><span><span style="color:#ba2121">
</span></span></span><span style="display:flex;"><span><span style="color:#ba2121">INPUT is the audio file, OUTPUT is the path to the resulting SRT file.&#34;</span>
</span></span></span><span style="display:flex;"><span><span style="color:#ba2121">INPUT is the audio file, OUTPUT-DIR is the path to the directory with
</span></span></span><span style="display:flex;"><span><span style="color:#ba2121">resulting files.&#34;</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">interactive</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">list</span>
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">read-file-name</span> <span style="color:#ba2121">&#34;Input file: &#34;</span> <span style="color:#800">nil</span> <span style="color:#800">nil</span> <span style="color:#800">t</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">read-file-name</span> <span style="color:#ba2121">&#34;SRT file: &#34;</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let*</span> ((<span style="color:#19177c">buffer</span> (<span style="color:#19177c">generate-new-buffer</span> <span style="color:#ba2121">&#34;vosk&#34;</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">default-directory</span> <span style="color:#19177c">my/vosk-script-path</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">read-directory-name</span> <span style="color:#ba2121">&#34;Output directory: &#34;</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let*</span> ((<span style="color:#19177c">buffer</span> (<span style="color:#19177c">generate-new-buffer</span> <span style="color:#ba2121">&#34;whisper&#34;</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">default-directory</span> <span style="color:#19177c">my/whisper-env-path</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">proc</span> (<span style="color:#00f">start-process</span>
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;vosk_api&#34;</span> <span style="color:#19177c">buffer</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">concat</span> <span style="color:#19177c">my/vosk-script-path</span> <span style="color:#ba2121">&#34;venv/bin/python&#34;</span>)
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;main.py&#34;</span> <span style="color:#ba2121">&#34;--file-path&#34;</span> <span style="color:#19177c">input</span> <span style="color:#ba2121">&#34;--model-path&#34;</span> <span style="color:#ba2121">&#34;./model-small&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;--save-path&#34;</span> <span style="color:#19177c">output</span> <span style="color:#ba2121">&#34;--words-per-line&#34;</span> <span style="color:#ba2121">&#34;14&#34;</span>)))
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;whisper&#34;</span> <span style="color:#19177c">buffer</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">concat</span> <span style="color:#19177c">my/whisper-env-path</span> <span style="color:#ba2121">&#34;venv/bin/whisper&#34;</span>)
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;--output_dir&#34;</span> <span style="color:#19177c">output-dir</span> <span style="color:#ba2121">&#34;--model&#34;</span> <span style="color:#ba2121">&#34;tiny.en&#34;</span> <span style="color:#19177c">input</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#00f">set-process-sentinel</span>
</span></span><span style="display:flex;"><span> <span style="color:#19177c">proc</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">lambda</span> (<span style="color:#19177c">process</span> <span style="color:#19177c">_msg</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let</span> ((<span style="color:#19177c">status</span> (<span style="color:#00f">process-status</span> <span style="color:#19177c">process</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">code</span> (<span style="color:#00f">process-exit-status</span> <span style="color:#19177c">process</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">cond</span> ((<span style="color:#008000">and</span> (<span style="color:#00f">eq</span> <span style="color:#19177c">status</span> <span style="color:#19177c">&#39;exit</span>) (<span style="color:#00f">=</span> <span style="color:#19177c">code</span> <span style="color:#666">0</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">notifications-notify</span> <span style="color:#008000">:body</span> <span style="color:#ba2121">&#34;SRT conversion completed&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000">:title</span> <span style="color:#ba2121">&#34;Vosk API&#34;</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">notifications-notify</span> <span style="color:#008000">:body</span> <span style="color:#ba2121">&#34;Audio conversion completed&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000">:title</span> <span style="color:#ba2121">&#34;Whisper&#34;</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#00f">kill-buffer</span> (<span style="color:#00f">process-buffer</span> <span style="color:#19177c">process</span>)))
</span></span><span style="display:flex;"><span> ((<span style="color:#008000">or</span> (<span style="color:#008000">and</span> (<span style="color:#00f">eq</span> <span style="color:#19177c">status</span> <span style="color:#19177c">&#39;exit</span>) (<span style="color:#00f">&gt;</span> <span style="color:#19177c">code</span> <span style="color:#666">0</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#00f">eq</span> <span style="color:#19177c">status</span> <span style="color:#19177c">&#39;signal</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let</span> ((<span style="color:#19177c">err</span> (<span style="color:#008000">with-current-buffer</span> (<span style="color:#00f">process-buffer</span> <span style="color:#19177c">process</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#00f">buffer-string</span>))))
</span></span><span style="display:flex;"><span> (<span style="color:#00f">kill-buffer</span> (<span style="color:#00f">process-buffer</span> <span style="color:#19177c">process</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#d2413a;font-weight:bold">user-error</span> <span style="color:#ba2121">&#34;Error in Vosk API: %s&#34;</span> <span style="color:#19177c">err</span>)))))))))
</span></span><span style="display:flex;"><span> (<span style="color:#d2413a;font-weight:bold">user-error</span> <span style="color:#ba2121">&#34;Error in Whisper: %s&#34;</span> <span style="color:#19177c">err</span>)))))))))
</span></span></code></pre></div><p>If run interactively, the defined function prompts for paths to both files.</p>
<p>The process sentinel sends a <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Desktop-Notifications.html">desktop notification</a> because it&rsquo;s a bit more noticeable than <code>message</code>, and the process is expected to take some time.</p>
<h5 id="integrating-with-elfeed">Integrating with elfeed</h5>
<p>To actually run the function from the section above, we need to download the file in question.</p>
<p>So first, let&rsquo;s extract the file name from the URL:</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/get-file-name-from-url</span> (<span style="color:#19177c">url</span>)
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;Extract file name from the URL.&#34;</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">string-match</span> (<span style="color:#008000">rx</span> <span style="color:#ba2121">&#34;/&#34;</span> (<span style="color:#00f">+</span> (<span style="color:#19177c">not</span> <span style="color:#ba2121">&#34;/&#34;</span>)) (<span style="color:#ba2121">? </span><span style="color:#ba2121">&#34;/&#34;</span>) <span style="color:#19177c">eos</span>) <span style="color:#19177c">url</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let</span> ((<span style="color:#19177c">match</span> (<span style="color:#19177c">match-string</span> <span style="color:#666">0</span> <span style="color:#19177c">url</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">unless</span> <span style="color:#19177c">match</span>
</span></span><span style="display:flex;"><span> (<span style="color:#d2413a;font-weight:bold">user-error</span> <span style="color:#ba2121">&#34;No file name found. Somehow&#34;</span>))
</span></span><span style="display:flex;"><span> <span style="color:#408080;font-style:italic">;; Remove the first /</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">setq</span> <span style="color:#19177c">match</span> (<span style="color:#00f">substring</span> <span style="color:#19177c">match</span> <span style="color:#666">1</span>))
</span></span><span style="display:flex;"><span> <span style="color:#408080;font-style:italic">;; Remove the trailing /</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">when</span> (<span style="color:#19177c">string-match-p</span> (<span style="color:#008000">rx</span> <span style="color:#ba2121">&#34;/&#34;</span> <span style="color:#19177c">eos</span>) <span style="color:#19177c">match</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#008000">setq</span> <span style="color:#19177c">match</span> (<span style="color:#00f">substring</span> <span style="color:#19177c">match</span> <span style="color:#666">0</span> (<span style="color:#00f">1-</span> (<span style="color:#00f">length</span> <span style="color:#19177c">match</span>)))))
</span></span><span style="display:flex;"><span> <span style="color:#19177c">match</span>))
</span></span></code></pre></div><p>I use a library called <a href="https://github.com/tkf/emacs-request">request.el</a> to download files elsewhere, so I&rsquo;ll re-use it here. You can just as well invoke <code>curl</code> or <code>wget</code> via a asynchronous process.</p>
<p>The <code>whisper</code> executable, given the file <code>&lt;file&gt;.&lt;extension&gt;</code>, creates files named <code>&lt;file&gt;.vtt</code>, <code>&lt;file&gt;.srt</code>, <code>&lt;file&gt;.txt</code>. So first we need to save the file under the correct name.</p>
<p>I use a library called <a href="https://github.com/tkf/emacs-request">request.el</a> to download files elsewhere, so I&rsquo;ll re-use it here. You can just as well invoke <code>curl</code> or <code>wget</code> via a asynchronous process.</p>
<p>This function downloads the file to a non-temporary folder, which is <code>~/.elfeed/podcast-files/</code> if you didn&rsquo;t move the elfeed database. That is so because a permanently downloaded file works better for the next section.</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">with-eval-after-load</span> <span style="color:#19177c">&#39;elfeed</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">defvar</span> <span style="color:#19177c">my/elfeed-vosk-podcast-files-directory</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">defvar</span> <span style="color:#19177c">my/elfeed-whisper-podcast-files-directory</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">concat</span> <span style="color:#19177c">elfeed-db-directory</span> <span style="color:#ba2121">&#34;/podcast-files/&#34;</span>)))
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/elfeed-vosk-get-transcript-new</span> (<span style="color:#19177c">url</span> <span style="color:#19177c">srt-path</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let*</span> ((<span style="color:#19177c">file-name</span> (<span style="color:#19177c">my/get-file-name-from-url</span> <span style="color:#19177c">url</span>))
</span></span><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/elfeed-whisper-get-transcript-new</span> (<span style="color:#19177c">entry</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#008000">interactive</span> (<span style="color:#00f">list</span> <span style="color:#19177c">elfeed-show-entry</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let*</span> ((<span style="color:#19177c">url</span> (<span style="color:#19177c">caar</span> (<span style="color:#19177c">elfeed-entry-enclosures</span> <span style="color:#19177c">entry</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">file-name</span> (<span style="color:#00f">concat</span>
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">elfeed-ref-id</span> (<span style="color:#19177c">elfeed-entry-content</span> <span style="color:#19177c">entry</span>))
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;.&#34;</span>
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">file-name-extension</span> <span style="color:#19177c">url</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">file-path</span> (<span style="color:#00f">expand-file-name</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">concat</span>
</span></span><span style="display:flex;"><span> <span style="color:#19177c">my/elfeed-vosk-podcast-files-directory</span>
</span></span><span style="display:flex;"><span> <span style="color:#19177c">my/elfeed-whisper-podcast-files-directory</span>
</span></span><span style="display:flex;"><span> <span style="color:#19177c">file-name</span>))))
</span></span><span style="display:flex;"><span> (<span style="color:#00f">message</span> <span style="color:#ba2121">&#34;Download started&#34;</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#008000">unless</span> (<span style="color:#00f">file-exists-p</span> <span style="color:#19177c">my/elfeed-vosk-podcast-files-directory</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">mkdir</span> <span style="color:#19177c">my/elfeed-vosk-podcast-files-directory</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">unless</span> (<span style="color:#00f">file-exists-p</span> <span style="color:#19177c">my/elfeed-whisper-podcast-files-directory</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">mkdir</span> <span style="color:#19177c">my/elfeed-whisper-podcast-files-directory</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">request</span> <span style="color:#19177c">url</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000">:type</span> <span style="color:#ba2121">&#34;GET&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#008000">:encoding</span> <span style="color:#19177c">&#39;binary</span>
@ -5807,14 +5802,35 @@ Emacs is also particularly good at writing Lisp code, e.g. Clojure, Common Lisp,
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">write-region-post-annotation-function</span> <span style="color:#800">nil</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#00f">write-region</span> <span style="color:#19177c">data</span> <span style="color:#800">nil</span> <span style="color:#19177c">file-path</span> <span style="color:#800">nil</span> <span style="color:#008000">:silent</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#00f">message</span> <span style="color:#ba2121">&#34;Conversion started&#34;</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">my/invoke-vosk</span> <span style="color:#19177c">file-path</span> <span style="color:#19177c">srt-path</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">my/invoke-whisper</span> <span style="color:#19177c">file-path</span> <span style="color:#19177c">my/elfeed-srt-dir</span>)))
</span></span><span style="display:flex;"><span> <span style="color:#008000">:error</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">cl-function</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">lambda</span> (<span style="color:#008000">&amp;key</span> <span style="color:#19177c">error-thrown</span> <span style="color:#008000">&amp;allow-other-keys</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#00f">message</span> <span style="color:#ba2121">&#34;Error!: %S&#34;</span> <span style="color:#19177c">error-thrown</span>))))))
</span></span></code></pre></div><p>I also experimented with a bunch of options to write binary data in Emacs, of which the way with <code>write-region</code> (as implemented in <a href="https://github.com/rejeep/f.el">f.el</a>) seems to be the fastest. <a href="https://emacs.stackexchange.com/questions/59449/how-do-i-save-raw-bytes-into-a-file">This thread on StackExchange</a> suggests that it may screw some bytes towards the end, but whether or not this is the case, mp3 files survive the procedure. The proposed solution with <code>seq-doseq</code> takes at least a few seconds.</p>
<p>Finally, we need a function to show the transcript if it exists or invoke <code>my/elfeed-vosk-get-transcript-new</code> if it doesn&rsquo;t. And this is the function that we&rsquo;ll call from an <code>elfeed-entry</code> buffer.</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/elfeed-vosk-get-transcript</span> (<span style="color:#19177c">entry</span>)
<p>As <code>my/invoke-whisper</code> creates multiple files, here&rsquo;s a function to select related files:</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/elfeed-show-related-files</span> (<span style="color:#19177c">entry</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#008000">interactive</span> (<span style="color:#00f">list</span> <span style="color:#19177c">elfeed-show-entry</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let*</span> ((<span style="color:#19177c">files</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">mapcar</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">lambda</span> (<span style="color:#19177c">file</span>) (<span style="color:#00f">cons</span> (<span style="color:#19177c">file-name-extension</span> <span style="color:#19177c">file</span>) <span style="color:#19177c">file</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">seq-filter</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">lambda</span> (<span style="color:#19177c">file</span>)
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">string-match-p</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">rx</span> <span style="color:#19177c">bos</span> (<span style="color:#19177c">literal</span> (<span style="color:#19177c">elfeed-ref-id</span> (<span style="color:#19177c">elfeed-entry-content</span> <span style="color:#19177c">entry</span>))) <span style="color:#ba2121">&#34;.&#34;</span>)
</span></span><span style="display:flex;"><span> <span style="color:#19177c">file</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#00f">directory-files</span> <span style="color:#19177c">my/elfeed-srt-dir</span>))))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">buffer</span>
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">find-file-other-window</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">concat</span>
</span></span><span style="display:flex;"><span> <span style="color:#19177c">my/elfeed-srt-dir</span>
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">alist-get</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">completing-read</span> <span style="color:#ba2121">&#34;File: &#34;</span> <span style="color:#19177c">files</span>)
</span></span><span style="display:flex;"><span> <span style="color:#19177c">files</span> <span style="color:#800">nil</span> <span style="color:#800">nil</span> <span style="color:#00f">#&#39;equal</span>)))))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">with-current-buffer</span> <span style="color:#19177c">buffer</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">setq-local</span> <span style="color:#19177c">elfeed-show-entry</span> <span style="color:#19177c">entry</span>))))
</span></span></code></pre></div><p>Finally, we need a function to show the transcript if it exists or invoke <code>my/elfeed-whisper-get-transcript-new</code> if it doesn&rsquo;t. And this is the function that we&rsquo;ll call from an <code>elfeed-entry</code> buffer.</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/elfeed-whisper-get-transcript</span> (<span style="color:#19177c">entry</span>)
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;Retrieve transcript for the enclosure of the current elfeed ENTRY.&#34;</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">interactive</span> (<span style="color:#00f">list</span> <span style="color:#19177c">elfeed-show-entry</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let</span> ((<span style="color:#19177c">enclosure</span> (<span style="color:#19177c">caar</span> (<span style="color:#19177c">elfeed-entry-enclosures</span> <span style="color:#19177c">entry</span>))))
@ -5827,13 +5843,13 @@ Emacs is also particularly good at writing Lisp code, e.g. Clojure, Common Lisp,
</span></span><span style="display:flex;"><span> (<span style="color:#008000">let</span> ((<span style="color:#19177c">buffer</span> (<span style="color:#19177c">find-file-other-window</span> <span style="color:#19177c">srt-path</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">with-current-buffer</span> <span style="color:#19177c">buffer</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">setq-local</span> <span style="color:#19177c">elfeed-show-entry</span> <span style="color:#19177c">entry</span>)))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">my/elfeed-vosk-get-transcript-new</span> <span style="color:#19177c">enclosure</span> <span style="color:#19177c">srt-path</span>)))))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">my/elfeed-whisper-get-transcript-new</span> <span style="color:#19177c">entry</span>)))))
</span></span></code></pre></div><h5 id="integrating-with-subed">Integrating with subed</h5>
<p>Now that we&rsquo;ve produced a <code>.srt</code> file, we can use a package called <a href="https://github.com/sachac/subed">subed</a> to control the playback, like I had done in the previous post.</p>
<p>Now that we&rsquo;ve produced a <code>.srt</code> file, we can use a package called <a href="https://github.com/sachac/subed">subed</a> to control the playback, as I had done in the previous post.</p>
<p>By the way, this wasn&rsquo;t the most straightforward thing to figure out, because the MPV window doesn&rsquo;t show up for an audio file, and the player itself starts in the paused state. So I thought nothing was happening until I enabled the debug log.</p>
<p>With that in mind, here&rsquo;s a function to launch MPV from the buffer generated by <code>my/elfeed-vosk-get-transcript</code>:</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/elfeed-vosk-subed</span> (<span style="color:#19177c">entry</span>)
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;Run MPV for the current Vosk-generated subtitles file.
<p>With that in mind, here&rsquo;s a function to launch MPV from the buffer generated by <code>my/elfeed-whisper-get-transcript</code>:</p>
<div class="highlight"><pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-emacs-lisp" data-lang="emacs-lisp"><span style="display:flex;"><span>(<span style="color:#008000">defun</span> <span style="color:#19177c">my/elfeed-whisper-subed</span> (<span style="color:#19177c">entry</span>)
</span></span><span style="display:flex;"><span> <span style="color:#ba2121">&#34;Run MPV for the current Whisper-generated subtitles file.
</span></span></span><span style="display:flex;"><span><span style="color:#ba2121">
</span></span></span><span style="display:flex;"><span><span style="color:#ba2121">ENTRY is an instance of </span><span style="color:#19177c">`elfeed-entry&#39;</span><span style="color:#ba2121">.&#34;</span>
</span></span><span style="display:flex;"><span> (<span style="color:#008000">interactive</span> (<span style="color:#00f">list</span> <span style="color:#19177c">elfeed-show-entry</span>))
@ -5843,18 +5859,18 @@ Emacs is also particularly good at writing Lisp code, e.g. Clojure, Common Lisp,
</span></span><span style="display:flex;"><span> (<span style="color:#d2413a;font-weight:bold">user-error</span> <span style="color:#ba2121">&#34;Not subed mode!&#34;</span>))
</span></span><span style="display:flex;"><span> (<span style="color:#008000">setq-local</span> <span style="color:#19177c">subed-mpv-video-file</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">expand-file-name</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">concat</span> <span style="color:#19177c">my/elfeed-vosk-podcast-files-directory</span>
</span></span><span style="display:flex;"><span> (<span style="color:#00f">concat</span> <span style="color:#19177c">my/elfeed-whisper-podcast-files-directory</span>
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">my/get-file-name-from-url</span>
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">caar</span> (<span style="color:#19177c">elfeed-entry-enclosures</span> <span style="color:#19177c">entry</span>))))))
</span></span><span style="display:flex;"><span> (<span style="color:#19177c">subed-mpv--play</span> <span style="color:#19177c">subed-mpv-video-file</span>))
</span></span></code></pre></div><p>After running <code>M-x my/elfeed-vosk-subed</code>, run <code>M-x subed-toggle-loop-over-current-subtitle</code> (<code>C-c C-l</code>), because somehow it&rsquo;s turned on by default, and <code>M-x subed-toggle-pause-while-typing</code> (<code>C-c C-p</code>), because sometimes this made my instance of MPV lag.</p>
</span></span></code></pre></div><p>After running <code>M-x my/elfeed-whisper-subed</code>, run <code>M-x subed-toggle-loop-over-current-subtitle</code> (<code>C-c C-l</code>), because somehow it&rsquo;s turned on by default, and <code>M-x subed-toggle-pause-while-typing</code> (<code>C-c C-p</code>), because sometimes this made my instance of MPV lag.</p>
<p>After that, <code>M-x subed-mpv-toggle-pause</code> should start the playback, which you can control by moving the cursor in the buffer.</p>
<p>You can also run <code>M-x subed-toggle-sync-point-to-player</code> (<code>C-c .</code>) to toggle syncing the point in the buffer to the currently played subtitle (this automatically gets disabled when you switch buffers).</p>
<p>Running <code>M-x subed-toggle-sync-player-to-point</code> (<code>C-c ,</code>) does the opposite, i.e. sets the player position to the subtitle under point. These two functions are useful since the MPV window controls aren&rsquo;t available.</p>
<h5 id="some-observations">Some observations</h5>
<p>So, the functions above work for my purposes.</p>
<p>I think it should be possible to get transcripts of better quality by using a better speech recognition model, adding a speaker detection model and a model to restore case &amp; punctuation. But it seems to be harder to implement, and this would take more time and resources. On my PC, the smallest Vosk model runs maybe 10 times faster than the playback time, which is still a few minutes for an hour-long podcast. Waiting longer is probably not worth it.</p>
<p>Also, technically MPV can stream files without downloading them, and it&rsquo;s even possible to feed stream data into Vosk. But MPV isn&rsquo;t particularly good at seeking in streamed files, at least not with my Internet connection.</p>
<p>Vosk API works much faster than Whisper. The smallest Vosk model requires ~10 times less than the playback time, and even the <code>tiny.en</code> Whisper model on my PC requires maybe 1.2x playback time.</p>
<p>However, the quality of the output for Whisper is just so much better so I consider it to be worth the wait. Even with the <code>tiny</code> model, the transcript is almost perfect, provided that the audio is of reasonable quality.</p>
<h3 id="internet-and-multimedia">Internet &amp; Multimedia</h3>
<h4 id="notmuch">Notmuch</h4>
<p>My notmuch config now resides in <a href="/configs/mail/">Mail.org</a>.</p>
@ -6935,6 +6951,7 @@ Emacs is also particularly good at writing Lisp code, e.g. Clojure, Common Lisp,
<li><a href="#review-workflow">Review workflow</a></li>
<li><a href="#org-contacts">org-contacts</a></li>
<li><a href="#managing-tables">Managing tables</a></li>
<li><a href="#copying-records">Copying records</a></li>
</ul>
</li>
<li><a href="#ui">UI</a>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 67 KiB