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.
154 lines
5.5 KiB
154 lines
5.5 KiB
#!/bin/bash |
|
# Озвучивание русского текста из файла pdf и сохранение в видео |
|
# Параметры |
|
emotion='neutral' #'Default is neutral. Also supported are good (friendly) and evil (angry)' |
|
speaker='ermil' # (Optional): Speaker voice. Default is zahar. Supported female voices are jane, oksana, alyss, omazh and male voices are zahar and ermil. |
|
#["oksana","jane","omazh","zahar","ermil","silaerkan","erkanyavas","alyss", "nick","alena","filipp"] |
|
speed='1.0' |
|
use_half="yes" # yes or no |
|
video_width=1920 |
|
video_height=1080 |
|
ffmpeg_pre_options="-loop 1 -r 2" |
|
ffmpeg_options="-c:v libx264 -tune stillimage -preset ultrafast -crf 20 -shortest -pix_fmt yuv420p -s ${video_width}*${video_height}" |
|
minimum_text_on_page=1000 |
|
minimum_time_on_page=5 |
|
|
|
#---------------------------------------------------- |
|
|
|
input_pdf_file_name="$1" |
|
|
|
out_file="$input_pdf_file_name.mp4" |
|
|
|
page_count=$(pdfinfo "$input_pdf_file_name" | awk '/^Pages:/ {print $2}') |
|
|
|
video_file_names_array=() |
|
|
|
function make_video { |
|
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 - ${time_play} )) |
|
time_opt="-c:a mp3 -af adelay=${add_time}s:all=true" # |
|
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}" |
|
} |
|
|
|
echo "Всего страниц $page_count" |
|
|
|
for ((page=1;page<=${page_count};page++)); do |
|
|
|
echo "------------------------------------------------" |
|
echo "Обрабатываем страницу №$page" |
|
|
|
page_text_file="${input_pdf_file_name}_${page}.txt" |
|
page_image_file="${input_pdf_file_name}_${page}" |
|
pdftotext -f $page -l $page "${input_pdf_file_name}" "$page_text_file" |
|
pdftoppm -r 300 -f $page -l $page -png -singlefile "${input_pdf_file_name}" "$page_image_file" |
|
|
|
page_image_file="${page_image_file}.png" |
|
|
|
source_text=$(cat "${page_text_file}") |
|
|
|
if [ "$use_half"="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_mp3_half1="${page_text_file}_half1" |
|
file_mp3_half2="${page_text_file}_half2" |
|
|
|
~/txt2mp3_by_yatts "${file_mp3_half1}" |
|
~/txt2mp3_by_yatts "${file_mp3_half2}" |
|
|
|
page_mp3_file_half1="${file_mp3_half1}_yatts.mp3" |
|
page_mp3_file_half2="${file_mp3_half2}_yatts.mp3" |
|
|
|
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_pdf_file_name}_${page}_half1.mp4" |
|
page_mp4_file_half2="${input_pdf_file_name}_${page}_half2.mp4" |
|
|
|
make_video "$page_image_file_half1" "$page_mp3_file_half1" "$page_mp4_file_half1" |
|
|
|
make_video "$page_image_file_half2" "$page_mp3_file_half2" "$page_mp4_file_half2" |
|
|
|
rm "$page_image_file_half1" |
|
rm "$page_image_file_half2" |
|
|
|
rm "$file_mp3_half1" |
|
rm "$file_mp3_half2" |
|
rm "$page_mp3_file_half1" |
|
rm "$page_mp3_file_half2" |
|
|
|
else |
|
~/txt2mp3_by_yatts "$page_text_file" |
|
|
|
page_mp3_file="${page_text_file}_yatts.mp3" |
|
|
|
page_mp4_file="${input_pdf_file_name}_${page}.mp4" |
|
|
|
make_video "$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="" |
|
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]}" |
|
echo "Удаляем файл '$f'" |
|
rm "$f" |
|
done |
|
IFS=$SAVE_IFS |
|
|
|
echo "Конечный файл создан '$out_file'!" |
|
|
|
|