You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
256 lines
9.9 KiB
256 lines
9.9 KiB
1 year ago
|
#!/bin/bash
|
||
|
# Общественное достояние, 2024, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_pdf2video@narod.ru>
|
||
|
|
||
|
# Озвучивание русского текста из файла pdf и сохранение в видео
|
||
|
|
||
|
version=1.0
|
||
|
|
||
|
# Формат:
|
||
|
# "Однобуквенная команда|Расширенная команда|Справка|Параметр|Значение по умолчанию|Команда на исполнение"
|
||
|
# Параметр: Пусто - нет параметров, : - есть параметр, :: - параметр не обязателен
|
||
|
|
||
|
common_params=(
|
||
|
"h|help|Посмотреть помощь.|||ShowHelp; exit;"
|
||
|
"v|version|Посмотреть версию программы.|||echo \$version; exit;"
|
||
|
"V|verbose|Подробный вывод.|||verbose=true"
|
||
|
# "|||||"
|
||
|
)
|
||
|
|
||
|
sound_params=(
|
||
|
"i|input|Входной текстовый файл.|:||"
|
||
|
"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'|"
|
||
|
# "|||:||"
|
||
|
)
|
||
|
|
||
|
video_params=(
|
||
|
"o|output|Выходной видео файл.|:|''|"
|
||
|
"k|split|Деление страницы пополам. Может быть либо 'yes', либо 'no'. По умолчанию '!DEFAULT!'.|:|'yes'|"
|
||
|
"W|video_width|Размер видео в пикселях по ширине. По умолчанию '!DEFAULT!'.|:|1920|"
|
||
|
"H|video_height|Размер видео в пикселях по высоте. По умолчанию '!DEFAULT!'.|:|1080|"
|
||
|
"p|ffmpeg_pre_options|Опции ffmpeg в самом начале. По умолчанию '!DEFAULT!'.|:|'-loop 1 -r 2'|"
|
||
|
"P|ffmpeg_options|Опции ffmpeg. По умолчанию '!DEFAULT!'.|:|'-c:v libx264 -tune stillimage -preset ultrafast -crf 20 -shortest -pix_fmt yuv420p'|"
|
||
|
"r|page_range|Указывает страницы из выходного файла для обработки. Пример '{1..32}', '{2..10..2}', '\$(seq 5 3 30)'|:|''|"
|
||
|
"m|minimum_text_on_page|Минимальное количество символов на странице при котором происходит разделение страницы на две. По умолчанию '!DEFAULT!'.|:|1000|"
|
||
|
# "|||:||"
|
||
|
)
|
||
|
|
||
|
all_params=("${common_params[@]}" "${sound_params[@]}" "${video_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")"
|
||
|
|
||
|
function ShowHelp() {
|
||
|
cat << EOF
|
||
|
Использование: pdf2video -i <text_file> [-o <mp4_file>] [-hV]
|
||
|
Озвучивание русского текста из файла pdf и сохранение в видео
|
||
|
|
||
|
Общие параметры
|
||
|
$(ProcessParams common_params Params2Help)
|
||
|
|
||
|
Параметры звука
|
||
|
$(ProcessParams sound_params Params2Help)
|
||
|
|
||
|
Параметры видео
|
||
|
$(ProcessParams video_params Params2Help)
|
||
|
EOF
|
||
|
}
|
||
|
|
||
|
# -------------------------------------------
|
||
|
|
||
|
while true
|
||
|
do
|
||
|
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\""
|
||
|
ShowHelp
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
[ "$out_file" = "" ] && { out_file="${input_file}.mp4"; [ $verbose ] && echo "Выходное имя файла \"$out_file\""; }
|
||
|
|
||
|
#----------------------------------------------------
|
||
|
|
||
|
page_count=$(pdfinfo "${input_file}" | awk '/^Pages:/ {print $2}')
|
||
|
|
||
|
video_file_names_array=()
|
||
|
|
||
|
function Text2mp3 {
|
||
|
local text_file=$1
|
||
|
local mp3_file=$2
|
||
|
verb=""
|
||
|
[ $verbose ] && verb="-V"
|
||
|
|
||
|
[ $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 {
|
||
|
local page_image_file=$1
|
||
|
local page_mp3_file=$2
|
||
|
local page_mp4_file=$3
|
||
|
|
||
|
local resized_page_image_file="${page_image_file}_resized.png"
|
||
|
|
||
|
ffmpeg -y -i "${page_image_file}" -vf "scale=${video_width}:${video_height}:force_original_aspect_ratio=decrease,pad=${video_width}:${video_height}:(ow-iw)/2:(oh-ih)/2" "${resized_page_image_file}"
|
||
|
|
||
|
local time_play=$(mp3info -p "%S\n" "${page_mp3_file}")
|
||
|
local time_opt="-c:a copy"
|
||
|
if [ ${minimum_time_on_page} -ge ${time_play} ]; then
|
||
|
local add_time=5 # $(( 5 - ${time_play} ))
|
||
|
time_opt="-c:a mp3 -af adelay=${add_time}s:all=true" #
|
||
|
[ $verbose ] && echo "time_opt ${time_opt}"
|
||
|
fi
|
||
|
|
||
|
ffmpeg ${ffmpeg_pre_options} -i "${resized_page_image_file}" -i "${page_mp3_file}" ${ffmpeg_options} ${time_opt} "${page_mp4_file}"
|
||
|
|
||
|
SAVE_IFS=$IFS
|
||
|
IFS=""
|
||
|
video_file_names_array+=(${page_mp4_file})
|
||
|
IFS=$SAVE_IFS
|
||
|
|
||
|
rm "${resized_page_image_file}"
|
||
|
}
|
||
|
|
||
|
[ $verbose ] && echo "Всего страниц $page_count"
|
||
|
|
||
|
for ((page=1;page<=${page_count};page++)); do
|
||
|
|
||
|
if [ $page_range ]; then
|
||
|
skip="true"
|
||
|
for p in $(eval echo "$page_range");
|
||
|
do
|
||
|
if [ $p = $page ]; then
|
||
|
skip="false"
|
||
|
break
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
if [ $skip = "true" ]; then
|
||
|
[ $verbose ] && echo "Пропускаем страницу №$page"
|
||
|
continue
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
[ $verbose ] && echo "------------------------------------------------"
|
||
|
[ $verbose ] && echo "Обрабатываем страницу №$page"
|
||
|
|
||
|
page_text_file="${input_file}_${page}.txt"
|
||
|
page_image_file="${input_file}_${page}"
|
||
|
pdftotext -f $page -l $page "${input_file}" "$page_text_file"
|
||
|
pdftoppm -r 300 -f $page -l $page -png -singlefile "${input_file}" "$page_image_file"
|
||
|
|
||
|
page_image_file="${page_image_file}.png"
|
||
|
|
||
|
source_text="$(cat "${page_text_file}")"
|
||
|
|
||
|
if [ "$split" = "yes" ] && [ ${#source_text} -ge $minimum_text_on_page ]; then
|
||
|
|
||
|
space_char=" "
|
||
|
split_size=$(( ${#source_text} / 2 + 2)) # Половина с небольшим запасом
|
||
|
file_index=0
|
||
|
for ((i=1;i<=${#source_text};i++)); do
|
||
|
cur_char=${source_text:$i-1:1}
|
||
|
cur_text="${cur_text}${cur_char}"
|
||
|
if [ "$cur_char" = "$space_char" ] && [ ${#cur_text} -ge $split_size ] || [ $i = ${#source_text} ]; then
|
||
|
let file_index+=1
|
||
|
|
||
|
echo "$cur_text" > "${page_text_file}_half${file_index}"
|
||
|
|
||
|
cur_text=""
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
file_txt_half1="${page_text_file}_half1"
|
||
|
file_txt_half2="${page_text_file}_half2"
|
||
|
|
||
|
page_mp3_file_half1="${file_txt_half1}.mp3"
|
||
|
page_mp3_file_half2="${file_txt_half2}.mp3"
|
||
|
|
||
|
Text2mp3 "$file_txt_half1" "$page_mp3_file_half1"
|
||
|
Text2mp3 "$file_txt_half2" "$page_mp3_file_half2"
|
||
|
|
||
|
width=$(identify -format "%w" "$page_image_file")> /dev/null
|
||
|
height=$(identify -format "%h" "$page_image_file")> /dev/null
|
||
|
|
||
|
height_half=$(( $height / 2 + $height / 20 ))
|
||
|
|
||
|
page_image_file_half1="${page_image_file}_half1.png"
|
||
|
page_image_file_half2="${page_image_file}_half2.png"
|
||
|
|
||
|
# format (widthxheight+left+top / wxh+l+t)
|
||
|
convert "$page_image_file" -crop ${width}x${height_half}+0+0 "$page_image_file_half1"
|
||
|
convert "$page_image_file" -crop ${width}x${height_half}+0+$(( $height - $height_half )) "$page_image_file_half2"
|
||
|
|
||
|
page_mp4_file_half1="${input_file}_${page}_half1.mp4"
|
||
|
page_mp4_file_half2="${input_file}_${page}_half2.mp4"
|
||
|
|
||
|
MakeVideo "$page_image_file_half1" "$page_mp3_file_half1" "$page_mp4_file_half1"
|
||
|
|
||
|
MakeVideo "$page_image_file_half2" "$page_mp3_file_half2" "$page_mp4_file_half2"
|
||
|
|
||
|
rm "$page_image_file_half1"
|
||
|
rm "$page_image_file_half2"
|
||
|
|
||
|
rm "$file_txt_half1"
|
||
|
rm "$file_txt_half2"
|
||
|
rm "$page_mp3_file_half1"
|
||
|
rm "$page_mp3_file_half2"
|
||
|
|
||
|
else
|
||
|
page_mp3_file="${page_text_file}.mp3"
|
||
|
|
||
|
Text2mp3 "$page_text_file" "$page_mp3_file"
|
||
|
|
||
|
page_mp4_file="${input_file}_${page}.mp4"
|
||
|
|
||
|
MakeVideo "$page_image_file" "$page_mp3_file" "$page_mp4_file"
|
||
|
|
||
|
rm "$page_mp3_file"
|
||
|
|
||
|
fi
|
||
|
|
||
|
rm "$page_image_file"
|
||
|
rm "$page_text_file"
|
||
|
|
||
|
done
|
||
|
|
||
|
SAVE_IFS=$IFS
|
||
|
IFS=""
|
||
|
[ $verbose ] && echo "Объединяем файлы ${video_file_names_array[*]} в $out_file"
|
||
|
ffmpeg -f concat -safe 0 -i <(for ((i = 0; i < ${#video_file_names_array[@]}; i++)) do echo "file '$PWD/${video_file_names_array[$i]}'"; done) -acodec copy -vcodec copy "$out_file"
|
||
|
|
||
|
for ((i = 0; i < ${#video_file_names_array[@]}; i++)) do
|
||
|
f="${video_file_names_array[$i]}"
|
||
|
[ $verbose ] && echo "Удаляем файл '$f'"
|
||
|
rm "$f"
|
||
|
done
|
||
|
IFS=$SAVE_IFS
|
||
|
|
||
|
[ $verbose ] && echo "Конечный файл создан '$out_file'!"
|
||
|
|