xubuntu4iran / vtt2srt

Convert vtt to srt subtitle format
BSD 3-Clause "New" or "Revised" License
4 stars 2 forks source link

vtt2srt is slow #2

Open milahu opened 2 years ago

milahu commented 2 years ago

i know this is micro optimization, but ...

vtt2srt takes 30 seconds for about 1000 subtitles

this script takes 0.03 seconds = 1000x faster

#! /usr/bin/env bash
# vtt2srt
# remove WEBVTT file header
# convert time format from ss.SSS to ss,SSS
# add empty hours, for example, convert 12:34.567 to 00:12:34,567
# keep existing index numbers
# todo: remove vtt comments
# todo: convert \r and \r\n to \n
# todo: add empty minutes
# license = MIT

set -eu

for i in "$@"
do

echo "i $i"

o="$i.srt"

<"$i" sed 's/^WEBVTT$//' \
| perl -p -00 -e 's/^(?:([0-9]+)\n)?([0-9]{2}:)?([0-9]{2}:[0-9]{2})\.([0-9]{3}) --> ([0-9]{2}:)?([0-9]{2}:[0-9]{2})\.([0-9]{3})\n/($1 ? $1 : ++$n).qq(\n).($2 ? $2 : q(00:)).$3.q(,).$4.q( --> ).($5 ? $5 : q(00:)).$6.q(,).$7.qq(\n)/e' \
>"$o"

echo "o $o"

done

... or simply use ffmpeg

ffmpeg -i i.vtt o.srt
xubuntu4iran commented 2 years ago

Thank you for your comment