Skip to content

Instantly share code, notes, and snippets.

@yradunchev
Last active October 9, 2019 03:43
Show Gist options
  • Select an option

  • Save yradunchev/db4e2a9084999f1cc19deafb222cddb5 to your computer and use it in GitHub Desktop.

Select an option

Save yradunchev/db4e2a9084999f1cc19deafb222cddb5 to your computer and use it in GitHub Desktop.
chapter podcast episode

id3tags.txt:

;FFMETADATA1
title="Title"
artist=Artist
album_artist=Artist
album=Album
copyright=Copyright 2019
track=01
genre=(186)
date=2019
encoder=Lavf58.29.100

[CHAPTER]
TIMEBASE=1/1
START=0
END=330
title=chapter \#1

[CHAPTER]
TIMEBASE=1/1
START=330
END=1530
title=chapter \#2

convert HH:MM:SS to SS

echo "0:25:30" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'

add chapters:

ffmpeg -i in.mp3 -i id3tags.txt -map_metadata 1 -c:a copy -id3v2_version 3 -write_id3v1 1 out.mp3

use script chptr:

Put chapter data in a file (in the example below file is named chapters):

0:0:14  0:2:15  глава едно
0:2:16  0:4:10  глава две
0:4:11  0:6:40  глава три

Format is:

start_time   end_time    chapter_title

Values are tab delimited. Run script as below:

./chptr title="my podcast" artist="the artist formerly known as Prince" \
pubdate="2019" copyright="cc-by" \
track="02" mp3=myfile.mp3 meta=chapters

Arguments:

title - episode tile
artist - podcast name
copyright - copyright notice 
track - episode number
pubdate - publication date
mp3 - audio file to be chaptered
meta - name of the file containing chapter data

Order of the arguments does not matter. Look for output file with_chapters.mp3

#!/bin/bash
# parse command line agruments
for arg in "$@"; do
key=$(echo $arg | cut -f1 -d=)
value=$(echo $arg | cut -f2 -d=)
case "$key" in
title) title=${value} ;;
artist) artist=${value} ;;
copyright) copyright=${value} ;;
track) track=${value} ;;
pubdate) dat=${value} ;;
meta) meta=${value} ;;
mp3) mp3=${value};;
*)
esac
done
# write header (genre 186=Podcast)
(
cat <<META
;FFMETADATA1
title=${title}
artist=${artist}
copyright=${copyright}
track=${track}
genre=(186)
date=${dat}
META
) > metadata.txt
# process chapter data
while read str end tit; do
#{ print ($1 * 3600) + ($2 * 60) + $3 }'
cstr=$(awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }' <(echo ${str}))
cend=$(awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }' <(echo ${end}))
(
cat <<CHAPTER
[CHAPTER]
TIMEBASE=1/1
START=${cstr}
END=${cend}
title=${tit}
CHAPTER
) >> metadata.txt
done < "$meta"
# write id3 tags
ffmpeg -i ${mp3} -i metadata.txt -map_metadata 1 -c:a copy -id3v2_version 3 -write_id3v1 1 with_chapters.mp3
rm metadata.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment