#+ELEVENTY_COLLECTIONS: _posts
#+ELEVENTY_BASE_DIR: ~/sync/static-blog/
#+ELEVENTY_BASE_URL: https://sachachua.com
#+TITLE: Post drafts
* EmacsConf: Resynchronizing audio and video using Emacs and subed-record  :emacs:subed:
:PROPERTIES:
:EXPORT_DATE: 2026-11-26T15:14:01-0500
:EXPORT_ELEVENTY_PERMALINK: /blog/2026/11/emacsconf-resynchronizing-audio-and-video-using-emacs-and-subed-record/
:EXPORT_ELEVENTY_FILE_NAME: blog/2026/11/emacsconf-resynchronizing-audio-and-video-using-emacs-and-subed-record/
:END:

Pedro Aranda and Roger Schurch used [[https://bbb.emacsverse.org][our instance]] of [[https://bigbluebutton.org][BigBlueButton]] to record their [[https://emacsconf.org/2026/talks/ltx-talk][upcoming presentation about accessible PDFs with ltx-talk]] for [[https://emacsconf.org/2026][EmacsConf 2026]]. I had configured it to export [[https://docs.bigbluebutton.org/development/recording/#deploying-other-formats][a single-file .webm]] for easier downloading and playback.
After their video finished processing, I downloaded it and I ran [[https://github.com/m-bain/whisperX][WhisperX]] with diarization on the video file.
I trimed the beginning and the ending to remove some extra bits.

While reviewing the transcript and adding chapter markers (using [[dotfun:sacha-subed-mpv-ocr-and-select-line-as-comment][sacha-subed-mpv-ocr-and-select-line-as-comment]]), I noticed that the audio and video tracks from BigBlueButton kept drifting apart throughout the video. I'm not entirely sure why. Even the original had these synchronization issues.

The desync bothered me enough that I wanted to sort it out. I could get the separate .webms for the audio, screenshare, and webcams, but their durations differed too, so I'd probably still need to figure out the offsets. I decided to work with the combined webm instead. I just needed to figure out the delays to add to the audio file to get it to match up. I'm not used to staring at people's faces trying to read their lips, but I'd had some practice when I corrected the offsets for the [[https://emacsconf.org/2026/talks/community/][Emacs community panel]]. That one is public. Now my process is:

1. Jump to a speaker change, since that's usually when the timestamps drift.
2. In MPV, use ~C-+~ or ~C--~ somewhat randomly until I happen across a setting that lined it up. I think it's + to add a delay to the audio, which is needed when the audio says the words first and then the video follows. ~-~ is for when the video is ahead of the audio. MPV displays the offset when I change it, and I can also use ~I~ to display the stats.
3. Once I've gotten things mostly synced, skim backwards until I find the end of the previous segment where the offset needs to be different.
4. Add the value as ~#+AV_OFFSET:~ directive in a comment in my VTT file. Ex:

   #+begin_example
   00:05:08.004 --> 00:05:11.531
   our teaching materials.

   NOTE #+AV_OFFSET: 600

   00:05:11.532 --> 00:05:14.353
   [Pedro]: And here in Europe we do have a
   #+end_example

5. Test it by selecting a region with a few subtitles and using ~subed-record-compile-video~ to check it. I used or ~mpv --loop-file=inf~ to loop over the full clip.
6. Test the audio file for the whole video without regenerating the video part: ~mpv video-file.webm --audio-file=test.webm~
7. Combine the new audio track with the video using ffmpeg, or regenerate the whole video with ~subed-record-compile-video~.

Here are the offsets I came up with:

| Offset |   Start time |     End time |
|--------+--------------+--------------|
|   -200 | 00:00:14.560 | 00:02:18.840 |
|    300 | 00:02:18.841 | 00:05:11.531 |
|    600 | 00:05:11.532 | 00:06:05.532 |
|    500 | 00:06:05.533 | 00:10:11.660 |
|    800 | 00:10:11.661 | 00:11:00.559 |
|   1000 | 00:11:00.560 | 00:13:17.451 |
|   1800 | 00:13:17.452 | 00:16:06.940 |
|   2500 | 00:16:06.941 | 00:18:16.704 |
|   3000 | 00:18:16.705 | 00:22:12.559 |
|   3600 | 00:22:12.560 | 00:26:31.406 |
|   3900 | 00:26:31.407 |          end |

I'm not entirely sure that I've gotten the offsets nailed down yet, but they're better than the original. Once the talk airs on Dec 12, you'll get to see the results at https://emacsconf.org/2026/talks/ltx-talk/ .

So now I have a workflow for resynchronizing audio and video using Emacs. I really liked being able to navigate the video by using the transcript. It was easy to find the next speaker change to examine closely. I liked not having to do lots of fiddly little splits on my clips, particularly since I often found myself needing to move the offset a little earlier or later.

My next steps are probably:

- modify ~compile-media.el~ to handle ~:av-offset~ for both open and closed captions
- modify subed-mpv to handle jumping to the right place
- modify ~subed-waveform~ to take the offset into account when displaying the waveform

Yay editing videos by using plain text!

