From 0a91d87456e270617d80db44290da46014d2a7c9 Mon Sep 17 00:00:00 2001 From: Gardouille Date: Wed, 14 Dec 2022 13:59:41 +0100 Subject: [PATCH] New ffmpeg scripts --- ff.extract.subs.sh | 23 +++++++++++++++++++++++ ff.to.x265.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100755 ff.extract.subs.sh create mode 100755 ff.to.x265.sh diff --git a/ff.extract.subs.sh b/ff.extract.subs.sh new file mode 100755 index 0000000..67f5aa6 --- /dev/null +++ b/ff.extract.subs.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# From : +# https://gist.github.com/kowalcj0/ae0bdc43018e2718fb75290079b8839a + +# Can be improve to call ffmpeg only one time : +# https://gist.github.com/kowalcj0/ae0bdc43018e2718fb75290079b8839a?permalink_comment_id=4187491#gistcomment-4187491 + +function subs() { + movie="${1}" + filename="${1%.*}" + mappings=`ffprobe -loglevel error -select_streams s -show_entries stream=index:stream_tags=language -of csv=p=0 "${movie}"` + OLDIFS=$IFS + IFS=, + ( while read -r idx lang + do + echo "Exctracting ${lang} subtitle #${idx} from ${movie}" + ffmpeg -nostdin -hide_banner -loglevel quiet -i "${movie}" -map 0:"$idx" "${filename}_${lang}_${idx}.srt" + done <<< "${mappings}" ) + IFS=$OLDIFS +} + +subs "${1}" diff --git a/ff.to.x265.sh b/ff.to.x265.sh new file mode 100755 index 0000000..f3683db --- /dev/null +++ b/ff.to.x265.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# +# Convert input file to x265 : +# * Keep only the first audio track (-map 0:a:0) +# * Keep all subtitles (-map 0:s) +# * Use fast preset for encoding +# * Simply add "x265" at the end of the file (before the extension) +# * Only print progression + +# Colors {{{ +readonly RED='\033[0;31m' +readonly RESET='\033[0m' +# }}} + +x265_convert() { + orig_file="${1}" + file_name=$(echo "${orig_file}" | sed -e 's/\.[^.]*$//') # without extension + #file_name="${1%.*}" # bash only + file_ext=$(echo "${orig_file}" | sed -e 's/.*\.//') # without extension + dest_file="${file_name}.x265.${file_ext}" + + ## If subtitles are available + if [ $(ffprobe -loglevel error -select_streams s -show_entries stream=index:stream_tags=language -of csv=p=0 "${orig_file}" | wc --lines) -eq "0" ]; then + printf "%b\n" "Try to convert original file (${orig_file}) with ffmpeg… without subtitles." + ffmpeg -loglevel quiet -stats -i "${orig_file}" -codec:v libx265 -preset fast -crf 28 -codec:a copy -map 0:v -map 0:a:0 "${dest_file}" + else + printf "%b\n" "Try to convert original file (${orig_file}) with ffmpeg… with subtitles." + ffmpeg -loglevel quiet -stats -i "${orig_file}" -codec:v libx265 -preset fast -crf 28 -codec:a copy -codec:s copy -map 0:v -map 0:a:0 -map 0:s "${dest_file}" + fi + + printf "%b\n" "New x265 file is available ( ${RED}${dest_file}${RESET} )." +} + +x265_convert "${1}"