Commit 376933c4 authored by Gökçe Aydos's avatar Gökçe Aydos
Browse files

chapter file gen

parent f98cf4c7
Pipeline #9916 passed with stage
in 6 minutes and 12 seconds
# generates .webvtt from ffmpeg_metadata_template
import argparse
import re
import subprocess # for calling ffprobe
# chapters file must the the following format, where D is a digit.
# D:DD:DD chapter
parser = argparse.ArgumentParser()
parser.add_argument('chapters')
parser.add_argument('ffmpeg_metadata_template')
parser.add_argument('video')
args = parser.parse_args()
# get video length
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", args.video],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
video_length = float(result.stdout)
chapters = list()
webvtt = 'WEBVTT\n'
with open(args.chapters) as f:
for line_number, line in enumerate(f):
regex = r"(\d{,2}):(\d{2}):(\d{2})\.?\d* (.*)"
x = re.match(regex, line)
try:
hrs = int(x.group(1))
mins = int(x.group(2))
secs = int(x.group(3))
title = x.group(4)
except AttributeError:
print('WARNING:')
print(f'{line_number}: {line} does not match to {regex}')
exit()
minutes = (hrs * 60) + mins
seconds = secs + (minutes * 60)
timestamp = (seconds * 1000)
chap = {
"title": title,
"startTime": timestamp
}
chapters.append(chap)
# end time must be greater than the start time
webvtt += f"""
{hrs:02}:{mins:02}:{secs:02}.000 --> {hrs:02}:{mins:02}:{secs:02}.001
{title}
"""
with open(args.video + '.vtt', "w") as f:
print('writing webvtt file')
f.write(webvtt)
ffmpeg_metadata = ""
for i, chapter in enumerate(chapters):
title = chapter['title']
start = chapter['startTime']
if i != len(chapters)-1:
end = chapters[i+1]['startTime']-1
else:
end = float(video_length)*1000
ffmpeg_metadata += f"""
[CHAPTER]
TIMEBASE=1/1000
START={start}
END={end}
title={title}
"""
with open(args.ffmpeg_metadata_template, "a") as f:
f.write(ffmpeg_metadata)
# integrates ffmpeg_chapter_file into the video
[ $# -lt 2 ] && echo "Usage: $0 VIDEO CHAPTERS" && exit 1
set -e
VIDEO=$1
VIDEONAME=$(basename $VIDEO)
CHAPTERS=$2
TEMPDIR=$(mktemp -d)
FFMETADATA=$TEMPDIR/$VIDEONAME.ffmpeg.metadata
set -x
ffmpeg -i $VIDEO -f ffmetadata $FFMETADATA
python apply-chapters-to-metadata.py $CHAPTERS $FFMETADATA $VIDEO
cat $FFMETADATA
ffmpeg -i $VIDEO -i $FFMETADATA -map_metadata 1 -codec copy ${VIDEO}-with-chapters.webm
rm -r $TEMPDIR
# gets the .chapters files
URL_PREFIX ?= https://mygit.th-deg.de/lsi/inf2-notes/-/raw/master/video-chapters
URL_SUFFIX ?= ?inline=false
%.chapters:
curl -L --fail -s --show-error \
"$(URL_PREFIX)/$*.chapters$(URL_SUFFIX)" -o $@
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment