DEV Community

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

Posted on

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

Top comments (0)