dotfiles

personal dotfiles
git clone anongit@rnpnr.xyz:dotfiles.git
Log | Files | Refs | Feed | Submodules

ass2srt (737B)


      1 #!/bin/awk -f
      2 
      3 # converts a Sub Station Alpha (ASS) subtitle file to SubRip (SRT)
      4 
      5 function tomsecs(a) {
      6 	gsub(/\./, ":", a)
      7 	split(a, t, ":")
      8 	return (t[1] * 3600 + t[2] * 60 + t[3]) * 1000 + t[4] * 10
      9 }
     10 
     11 function ftime(a) {
     12 	ms = a % 1000
     13 	a = (a - ms) / 1000
     14 	s = a % 60
     15 	a = (a - s) / 60
     16 	m = a % 60
     17 	h = (a - m) / 60
     18 
     19 	return sprintf("%02d:%02d:%02d,%03d", h, m, s, ms)
     20 }
     21 
     22 BEGIN {
     23 	FS = ","
     24 }
     25 
     26 !/^Dialogue: / {
     27 	next
     28 }
     29 
     30 {
     31 	start = tomsecs($2)
     32 	end = tomsecs($3)
     33 	if (end - start != 0) {
     34 		if (NF > 10)
     35 			gsub(/[^}]+}/, "", $NF)
     36 		subs[start] = sprintf("%s\n%s", ftime(end), $NF)
     37 	}
     38 }
     39 
     40 END {
     41 	n = asorti(subs, idxs, "@ind_num_asc")
     42 	for (i = 1; i <= n; i++) {
     43 		print i
     44 		printf("%s --> %s\n\n", ftime(idxs[i]), subs[idxs[i]])
     45 	}
     46 }