/
telnov.ob
/
cerberus
Обзор
Документация
Войти
/
telnov.ob
/
cerberus
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
shell-scripts/subs2transcript
84 строки
2 KB
Daniel J Wilcox
old shell scripts
21 янв 2026, 15:43
21 янв 2026, 15:43
dfaba3d
Код
Авторство
О чём код?
#!/bin/sh # convert subtitles to a transcript # script usage usage() { echo "\ # convert subtitles to a transcript $(basename "$0") -i infile.(srt|vtt)" exit 2 } # error messages NOTFILE_ERR='not a file' INVALID_OPT_ERR='Invalid option:' REQ_ARG_ERR='requires an argument' WRONG_ARGS_ERR='wrong number of arguments passed to script' FILE_EXT_ERR='has the wrong file extension' NOT_TEXT_FILE_ERR='is not a text file' # if script is run arguments pass and check the options with getopts, # else display script usage and exit [ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}" # getopts check and validate options while getopts ':i:o:h' opt do case ${opt} in i) infile="${OPTARG}" [ -f "${infile}" ] || usage "${infile} ${NOTFILE_ERR}";; o) outfile="${OPTARG}";; h) usage;; \?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;; :) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;; esac done shift $((OPTIND-1)) # infile, infile name and extension infile_nopath="${infile##*/}" infile_name="${infile_nopath%.*}" infile_ext="${infile##*.}" # text file regular expressions for case statement srt='[Ss][Rr][Tt]' vtt='[Vv][Tt][Tt]' # file command check input file mime type infile_check="$(file --mime-type -b "${infile}")" # text mimetype subs_mime='text/plain' # check the text file mime type is a text case "${infile_check}" in ${subs_mime});; *) usage "${infile_check} ${NOT_TEXT_FILE_ERR}";; esac # outfile file recording destination outfile_default="${infile_name}-transcript-$(date +"%Y-%m-%d-%H-%M-%S").txt" # srt subs srt_subs () { sed 's/^\([0-9:.,[:space:]>-]\)*//;s/ \{1,\}/ /g;s/\.[[:space:]]/./g;/^$/d' "${infile}" \ | awk -v RS="." '!/^\n$/ { print $0 "." }' ORS='\n\n' \ | fmt -sp -w 120 > "${outfile:=${outfile_default}}" } # vtt subs vtt_subs () { sed '1,3d;s/^\([0-9:.[:space:]>-]\)*//;s/ \{1,\}/ /g;s/\.[[:space:]]/./g;/^$/d' "${infile}" \ | awk -v RS="." '!/^\n$/ { print $0 "." }' ORS='\n\n' \ | fmt -sp -w 120 > "${outfile:=${outfile_default}}" } # check the subtitle file extension before passing to the correct function case "${infile_ext}" in ${srt}) srt_subs "${infile}";; ${vtt}) vtt_subs "${infile}";; *) usage "${infile} ${FILE_EXT_ERR}";; esac