DEV Community

Cover image for Add chapter markers to podcast/audio using FFmpeg
Josh Chen
Josh Chen

Posted on

3 2

Add chapter markers to podcast/audio using FFmpeg

Chapter markers are an easier way for listeners to navigate your podcast, see what's coming up, or skip over spoilers they don't want to hear. So I want to add the support for users to add chapter markers on JustCast. To do this, I turn to my weapons of choice, FFmpeg (v4.4).

Add chapter markers

FFmpeg can take metadata from a file and save it to the audio file.

ffmpeg -i "input-ep1-hello-world.mp3" -i "ep1-hello-world-metadata.txt" -map_metadata 1 -codec copy "output-ep1-hello-world.mp3"
Enter fullscreen mode Exit fullscreen mode

ep1-hello-world-metadata.txt

;FFMETADATA1

[CHAPTER]
TIMEBASE=1/1000
START=0
END=60000
title=chapter 1

[CHAPTER]
TIMEBASE=1/1
START=60
END=90
title=chapter 2
Enter fullscreen mode Exit fullscreen mode

We need to ensure two things: 1. end time needs to be greater than the start time, 2. the chapter start time needs to be greater than the last chapter end time.

https://ffmpeg.org/ffmpeg-formats.html#Metadata-1

fluent-ffmpeg

Fluent-FFmpeg

ffmpeg(url)
      .input(metafile)
      .audioCodec('copy')
      // .outputOptions('-metadata', 'title=song x')
      .outputOptions([
        "-map_metadata 0" // # 0 means copy whatever in the existing meta, 1 means ignore the existing
      ])
      .toFormat('mp3')
      .saveToFile(this.outputFilePath)   
      .on('codecData', (audioData) => {
        // console.log(getSecondsFromHHMMSS(audioData.duration))
        this.duration = getSecondsFromHHMMSS(audioData.duration);
      })   
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay