Browse Source

Библиотека для обработки аргументов

master
parent
commit
469e703b20
  1. 104
      Script/parse_arg_lib
  2. 106
      Script/pdf2video
  3. 133
      Script/txt2mp3

104
Script/parse_arg_lib

@ -0,0 +1,104 @@
#!/bin/bash
# Общественное достояние, 2024, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_pdf2video@narod.ru>
# Обработка входных параметров
# Формат:
# "Однобуквенная комманда|Расширенная комманда|Справка|Параметр|Значение по умолчанию|Команда на исполнение"
# Параметр: Пусто - нет параметров, : - есть параметр, :: - параметр не обязателен
# Пример
#common_params=(
# "i|input|Входной текстовый файл.|:|'1.txt'|"
# )
function ProcessParams {
local iparams=$1[@]
local work_func=$2
local params=("${!iparams}")
local custom_arg1="$3"
local custom_arg2="$4"
SAVE_IFS=$IFS
IFS=""
for (( i=0; i< ${#params[*]}; i++))
do
p="${params[i]}"
readarray -d "|" -t cur_params <<< "$p"
small_cmd="${cur_params[0]}"
large_cmd="${cur_params[1]}"
comment="${cur_params[2]}"
use_param="${cur_params[3]}"
default="${cur_params[4]}"
custom_cmd="${cur_params[5]}"
$work_func "$small_cmd" "$large_cmd" "$comment" "$use_param" "$default" "$custom_cmd" "$custom_arg1" "$custom_arg2"
done
IFS=$SAVE_IFS
}
function Params2InitVar {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
if [ "$use_param" != '' ]; then
eval "${large_cmd}=${default}"
fi
}
function Params2Help {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
echo "-${small_cmd}, -${large_cmd}, --${large_cmd}"
echo " ${comment/"!DEFAULT!"/"${!large_cmd}"}"
}
function Params2small_list {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
echo -n "${small_cmd}${use_param}"
}
function Params2large_list {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
echo -n ",${large_cmd}${use_param}"
}
function Params2Case {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
local custom_arg1="$7"
local custom_arg2="$8"
if [ "$custom_arg1" = "-${small_cmd}" ] || [ "$custom_arg1" = "-${large_cmd}" ] || [ "$custom_arg1" = "--${large_cmd}" ]; then
clear_custom_cmd="${custom_cmd//[$'\t\r\n ']/}"
if [ "${clear_custom_cmd}" != "" ]; then
eval "${custom_cmd}"
else
eval "${large_cmd}=\"$custom_arg2\""
fi
fi
}

106
Script/pdf2video

@ -42,75 +42,18 @@ video_params=(
all_params=("${common_params[@]}" "${sound_params[@]}" "${video_params[@]}")
function ProcessParams {
local iparams=$1[@]
local work_func=$2
local params=("${!iparams}")
local custom_arg1="$3"
local custom_arg2="$4"
SAVE_IFS=$IFS
IFS=""
for (( i=0; i< ${#params[*]}; i++))
do
p="${params[i]}"
readarray -d "|" -t cur_params <<< "$p"
small_cmd="${cur_params[0]}"
large_cmd="${cur_params[1]}"
comment="${cur_params[2]}"
use_param="${cur_params[3]}"
default="${cur_params[4]}"
custom_cmd="${cur_params[5]}"
$work_func "$small_cmd" "$large_cmd" "$comment" "$use_param" "$default" "$custom_cmd" "$custom_arg1" "$custom_arg2"
done
IFS=$SAVE_IFS
}
function Params2InitVar {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
if [ "$use_param" != '' ]; then
eval "${large_cmd}=${default}"
fi
}
# Загружаем библиотеку
function GetExec {
local exec_file_name="$1"
function Params2Help {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
echo "-${small_cmd}, -${large_cmd}, --${large_cmd}"
echo " ${comment/"!DEFAULT!"/"${!large_cmd}"}"
}
exec="$exec_file_name"
[ ! -f "$exec" ] && exec="./$exec_file_name"
[ ! -f "$exec" ] && exec="~/$exec_file_name"
function Params2small_list {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
echo -n "${small_cmd}${use_param}"
echo "$exec"
}
function Params2large_list {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
echo -n ",${large_cmd}${use_param}"
}
eval "source $(GetExec "parse_arg_lib")"
# Инициализация переменных
ProcessParams all_params Params2InitVar
@ -153,26 +96,6 @@ options=$(getopt --long "$large_params_list" -o "$small_params_list" -a -- "$@")
# are set to the arguments, even if some of them begin with a ‘-’.
eval set -- "$options"
function Params2Case {
local small_cmd="$1"
local large_cmd="$2"
local comment="$3"
local use_param="$4"
local default="$5"
local custom_cmd="$6"
local custom_arg1="$7"
local custom_arg2="$8"
if [ "$custom_arg1" = "-${small_cmd}" ] || [ "$custom_arg1" = "-${large_cmd}" ] || [ "$custom_arg1" = "--${large_cmd}" ]; then
clear_custom_cmd="${custom_cmd//[$'\t\r\n ']/}"
if [ "${clear_custom_cmd}" != "" ]; then
eval "${custom_cmd}"
else
eval "${large_cmd}=\"$custom_arg2\""
fi
fi
}
while true
do
cur_arg="$1"
@ -199,23 +122,14 @@ page_count=$(pdfinfo "${input_file}" | awk '/^Pages:/ {print $2}')
video_file_names_array=()
function GetExec {
local exec_file_name=$1
exec="$exec_file_name"
[ ! -f "$exec" ] && exec="./$exec"
[ ! -f "$exec" ] && exec="~/$exec"
echo "$exec"
}
function Text2mp3 {
local text_file=$1
local mp3_file=$2
verb=""
[ $verbose ] && verb="-V"
$(GetExec "txt2mp3") -i "$text_file" -o "$mp3_file" -e $emotion -s $speaker -S $speed -f $format -q $quality -l $lang $verb
[ $verbose ] && echo "Найден исполняемый файл для преобразования в звук текста $(GetExec txt2mp3)"
eval "$(GetExec "txt2mp3") -i '${text_file}' -o '${mp3_file}' -e '${emotion}' -s '${speaker}' -S '${speed}' -f '${format}' -q '${quality}' -l '${lang}' '${verb}'"
}
function MakeVideo {

133
Script/txt2mp3

@ -3,36 +3,60 @@
# Озвучивание текста из файла
# Параметры по умолчанию
emotion='neutral'
speaker='erkanyavas'
speed='1.0'
verbose=
ffmpeg_opt=""
version=1.0
input_file=""
out_file=""
format="mp3"
quality="hi"
lang="ru_RU"
ShowHelp() {
# Формат:
# "Однобуквенная комманда|Расширенная комманда|Справка|Параметр|Значение по умолчанию|Команда на исполнение"
# Параметр: Пусто - нет параметров, : - есть параметр, :: - параметр не обязателен
common_params=(
"h|help|Посмотреть помощь.|||ShowHelp; exit;"
"v|version|Посмотреть версию программы.|||echo \$version; exit;"
"V|verbose|Подробный вывод.|||verbose=true"
# "|||||"
)
sound_params=(
"i|input|Входной текстовый файл.|:||"
"o|output|Выходной видео файл.|:|''|"
"e|emotion|Эмоциональный настрой говорящего. Может принимать значения 'neutral', 'good', 'evil'. По умолчанию '!DEFAULT!'.|:|'neutral'|"
"s|speaker|Голос говорящего. Может принимать значения 'oksana','jane','omazh','zahar','ermil','silaerkan','erkanyavas','alyss', 'nick'. По умолчанию '!DEFAULT!'.|:|'erkanyavas'|"
"S|speed|Скорость озвучки. По умолчанию '!DEFAULT!'.|:|'1.0'|"
"O|ffmpeg_opt|Дополнительные параметры ffmpeg.|:|''|"
"f|format|Выходной формат. Может быть либо 'mp3', либо 'wav'. По умолчанию '!DEFAULT!'.|:|'mp3'|"
"q|quality|Качество выходного файла. Может быть либо 'hi', либо 'lo'. По умолчанию '!DEFAULT!'.|:|'hi'|"
"l|lang|Язык озвучки. По умолчанию '!DEFAULT!'.|:|'ru_RU'|"
# "|||:||"
)
all_params=("${common_params[@]}" "${sound_params[@]}")
# Загружаем библиотеку
function GetExec {
local exec_file_name="$1"
exec="$exec_file_name"
[ ! -f "$exec" ] && exec="./$exec_file_name"
[ ! -f "$exec" ] && exec="~/$exec_file_name"
echo "$exec"
}
eval "source $(GetExec "parse_arg_lib")"
# Инициализация переменных
ProcessParams all_params Params2InitVar
function ShowHelp() {
cat << EOF
Использование: txt2mp3 -i <text_file> [-o <mp3_file>] [-hV]
Озвучивание текста
-h, -help, --help Посмотреть помощь.
-v, -version, --version Посмотреть версию программы.
-i, -input, --input Входной текстовый файл.
-o, -output, --output Выходной файл звуковой.
-e, -emotion, --emotion Эмоциональный настрой говорящего. Может принимать значения "neutral", "good", "evil". По умолчанию "$emotion".
-s, -speaker, --speaker Голос говорящего. Может принимать значения "oksana","jane","omazh","zahar","ermil","silaerkan","erkanyavas","alyss", "nick". По умолчанию "$speaker".
-S, -speed, --speed Скорость озвучки. По умолчанию "$speed".
-O, -ffmpeg_opt, --ffmpeg_opt Дополнительные параметры ffmpeg.
-f, -format, --format Выходной формат. Может быть либо "mp3", либо "wav". По умолчанию "$format".
-q, -quality, --quality Качество выходного файла. Может быть либо "hi", либо "lo". По умолчанию "$quality".
-l, -lang, --lang Язык озвучки. По умолчанию "$lang".
-V, -verbose, --verbose Подробный вывод.
Использование: pdf2mp3 -i <text_file> [-o <mp4_file>] [-hV]
Озвучивание текста из файла
Общие параметры
$(ProcessParams common_params Params2Help)
Параметры звука
$(ProcessParams sound_params Params2Help)
EOF
}
@ -45,9 +69,13 @@ EOF
# 'h' is a no-value option.
# 'v:' implies that option -v has value and is a mandatory option. ':' means has a value.
# 't::' implies that option -t has value but is optional. '::' means optional.
small_params_list=$(ProcessParams all_params Params2small_list)
small_params_list="${small_params_list:1}"
large_params_list=$(ProcessParams all_params Params2large_list)
large_params_list="${large_params_list:1}"
options=$(getopt --long "help,version,verbose,input:,output:,emotion:,speaker:,speed:,ffmpeg_opt:,format:,quality:,lang:" -o "hvVi:o:e:s:S:O:f:q:l:" -a -- "$@")
options=$(getopt --long "$large_params_list" -o "$small_params_list" -a -- "$@")
# set --:
# If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters
@ -56,52 +84,15 @@ eval set -- "$options"
while true
do
case "$1" in
-h|--help)
ShowHelp
exit
;;
-v|--version)
echo $version
exit
;;
-V|--verbose)
verbose=true
;;
-i|--input)
input_file="$2"
;;
-o|--output)
out_file="$2"
;;
-e|--emotion)
emotion="$2"
;;
-s|--speaker)
speaker="$2"
;;
-S|--speed)
speed="$2"
;;
-O|--ffmpeg_opt)
ffmpeg_opt="$2"
;;
-f|--format)
format="$2"
;;
-q|--quality)
quality="$2"
;;
-l|--lang)
lang="$2"
;;
--)
shift
break;;
esac
cur_arg="$1"
[ "$cur_arg" = '--' ] && { shift; break; }
ProcessParams all_params Params2Case "$cur_arg" "$2"
shift
done
input_file="$input"
out_file="$output"
unuse_param="$*"
if [ "${input_file}" = "" ] || [ "$unuse_param" != "" ]; then
[ "$unuse_param" != "" ] && echo "Параметры не расшифрованы \"$unuse_param\""

Loading…
Cancel
Save