You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
convert265/265

148 lines
8.2 KiB
Plaintext

2 years ago
#!/bin/bash
1 year ago
# .INFO
# This will try to use your gpu AMD/INTEL to transcode video into X265
# The will also remux existing video to a streaming optimized mp4 container
# If it can remux your video it will extract subtitles and delete the mkv container
# .FYI
# QSV and libfdk_aac support requires compiling your own ffmpeg
# --enable-gpl --enable-version3 --enable-gnutls --enable-libaom --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-libfdk-aac --enable-libmfx
1 year ago
# .LICENSE [MIT]
# Copyright © 2023 Hugh Smalley
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 years ago
set -o pipefail
IFS=$'\n\t'
1 year ago
pro_dir="/opt/amdgpu-pro/etc/vulkan/icd.d"
export LD_LIBRARY_PATH="/opt/amdgpu/libdrm/lib64:/opt/amdgpu/libdrm/lib32:$LD_LIBRARY_PATH"
export VK_ICD_FILENAMES="${pro_dir}/amd_icd32.json:${pro_dir}/amd_icd64.json:$VK_ICD_FILENAMES"
# Selects english subs failing that it will get all subs
1 year ago
function get_subtitles() {
# Try to get english subs if not then get all of them
jq -r '.streams[] | select(.codec_type=="subtitle") | select(.tags.language | contains("en")) | .index' <"${video}.json" ||
jq -r '.streams[] | select(.codec_type=="subtitle") | .index' <"${video}.json"
}
# Get all the info needed on the video file
1 year ago
function getinfo() {
if [[ ! -f "${video}.json" ]]; then
ffprobe -v quiet -print_format json -show_streams "${video}" >"${video}.json"
fi
d=$(dirname "${video}")
filename=$(basename "${video}")
1 year ago
filename="${filename%.*}"
1 year ago
video_codec=$(jq -r '.streams[0].codec_name' <"${video}.json")
video_height=$(jq -r '.streams[0].height' <"${video}.json")
sub_indexes=$(get_subtitles)
video_size=$(stat -c%s "${video}")
video_size_75=$((video_size * 75 / 100))
1 year ago
video_size_50=$((video_size * 50 / 100))
_filename=$(echo "${filename}" | sed -e 's/h264/x265/g' -e 's/x264/x265/g' -e 's/AVC/x265/g' -e 's/XviD/x265/g')
echo -e "${video} // ${video_codec} // ${video_height} // ${video_size}"
}
# Extract subtitles. Tries other formats if possible. Uses .sup as last option
1 year ago
function extract_subtitles() {
for subtitle in ${sub_indexes}; do
1 year ago
nice -n 18 ffmpeg -hide_banner -loglevel error -n -threads 4 -i "${video}" -map "0:${subtitle}" "${d}/${_filename}_${subtitle}.srt" ||
nice -n 18 ffmpeg -hide_banner -loglevel error -n -threads 4 -i "${video}" -map "0:${subtitle}" "${d}/${_filename}_${subtitle}.vtt" ||
nice -n 18 ffmpeg -hide_banner -loglevel error -n -threads 4 -i "${video}" -map "0:${subtitle}" "${d}/${_filename}_${subtitle}.ass" ||
nice -n 18 ffmpeg -hide_banner -loglevel error -n -threads 4 -i "${video}" -map "0:${subtitle}" -c copy "${d}/${_filename}_${subtitle}.sup"
1 year ago
done
}
# Use VAAPI to transcode video into memory then move it back to disk
function convert_video_vappi_memory() {
1 year ago
nice -n 19 ffmpeg -hide_banner -loglevel quiet -y -threads 4 -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 -analyzeduration 20000000 -probesize 20000000 \
1 year ago
-i "${video}" -map 0:v -map 0:a -map 0:s? -map 0:d? -map 0:t? -c:s copy -c:a copy \
-vf 'scale_vaapi=format=p010,scale_vaapi=w=1920:-2' -c:v hevc_vaapi -profile:v main10 \
-max_muxing_queue_size 1024 -movflags +faststart -movflags use_metadata_tags "/dev/shm/${_filename}.mkv" || exit 1
mv -vu "/dev/shm/${_filename}" "${d}/${_filename}" || rm -f "/dev/shm/${_filename}.mkv"
1 year ago
file_size=$(stat -c%s "${d}/${_filename}.mkv")
# To account for the space space savings of being converted
1 year ago
if ((file_size >= video_size_50)); then
cleanup
fi
}
# Use VAAPI to transcode video
function convert_video_vaapi() {
1 year ago
nice -n 19 ffmpeg -hide_banner -loglevel quiet -y -threads 4 -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 -analyzeduration 20000000 -probesize 20000000 \
1 year ago
-i "${video}" -map 0:v -map 0:a -map 0:s? -map 0:d? -map 0:t? -c:s copy -c:a copy \
-vf 'scale_vaapi=format=p010,scale_vaapi=w=1920:-2' -c:v hevc_vaapi -profile:v main10 \
-max_muxing_queue_size 1024 -movflags +faststart -movflags use_metadata_tags "${d}/${_filename}.mkv" || exit 1
1 year ago
file_size=$(stat -c%s "${d}/${_filename}.mkv")
# To account for the space space savings of being converted
1 year ago
if ((file_size >= video_size_50)); then
cleanup
fi
}
# Use Quick Sync to transcode video into memory then move it back to disk
1 year ago
function convert_video_qsv_memory() {
1 year ago
nice -n 19 ~/Downloads/ffmpeg/ffmpeg -hide_banner -loglevel quiet -y -threads 4 -hwaccel qsv -hwaccel_output_format qsv -analyzeduration 20000000 -probesize 20000000 \
1 year ago
-i "${video}" -map 0:v -map 0:a -map 0:s? -map 0:d? -map 0:t? -c:s copy -c:a copy \
1 year ago
-c:v hevc_qsv -low_power false -preset 4 -profile:v 1 -scenario 3 -look_ahead 1 -global_quality 20 \
-max_muxing_queue_size 1024 -movflags +faststart -movflags use_metadata_tags "/dev/shm/${_filename}.mkv" || exit 1
1 year ago
mv -vu "/dev/shm/${_filename}" "${d}/${_filename}" || rm -f "/dev/shm/${_filename}.mkv"
1 year ago
file_size=$(stat -c%s "${d}/${_filename}.mkv")
# To account for the space space savings of being converted
1 year ago
if ((file_size >= video_size_50)); then
cleanup
fi
}
# Use Quick Sync to transcode video
1 year ago
function convert_video_qsv() {
1 year ago
nice -n 19 ~/Downloads/ffmpeg/ffmpeg -hide_banner -loglevel quiet -y -threads 4 -hwaccel qsv -hwaccel_output_format qsv -analyzeduration 20000000 -probesize 20000000 \
1 year ago
-i "${video}" -map 0:v -map 0:a -map 0:s? -map 0:d? -map 0:t? -c:s copy -c:a copy \
1 year ago
-c:v hevc_qsv -low_power false -preset 4 -profile:v 1 -scenario 3 -look_ahead 1 -global_quality 20 \
-max_muxing_queue_size 1024 -movflags +faststart -movflags use_metadata_tags "${d}/$_filename.mkv" || exit 1
1 year ago
file_size=$(stat -c%s "${d}/${_filename}.mkv")
# To account for the space space savings of being converted
1 year ago
if ((file_size >= video_size_50)); then
cleanup
fi
}
# Remux video to mp4 while dropping upsupported codecs like FLAC, OPUS, etc. You can end up with files that have no audio with this option
function remux_video_drop_unsupported() {
1 year ago
nice -n 19 ffmpeg -hide_banner -loglevel quiet -y -threads 4 -i "${video}" -c: copy -movflags +faststart -movflags use_metadata_tags "${d}/${_filename}.mp4" || exit 1
extract_subtitles
1 year ago
file_size=$(stat -c%s "${d}/${_filename}.mp4")
# To account for the loss of audio that is unsupported
if ((file_size >= video_size_75)); then
cleanup
fi
}
# Remux the video file to mp4 cotainers. Most failures are caused by audio that can not be stored in a mp4 container. So this will transcode audio to AAC when needed.
function remux_video() {
1 year ago
nice -n 19 ffmpeg -hide_banner -loglevel quiet -y -threads 4 -i "${video}" -map 0:v -map 0:a -c: copy -movflags +faststart -movflags use_metadata_tags "${d}/${_filename}.mp4" ||
nice -n 19 ~/Downloads/ffmpeg/ffmpeg -hide_banner -loglevel quiet -y -threads 4 -i "${video}" -map 0:v -map 0:a -c:v copy \
-c:a libfdk_aac -b:a 384k -movflags +faststart -movflags use_metadata_tags "${d}/${_filename}.mp4" || exit 1
file_size=$(stat -c%s "${d}/${_filename}.mp4")
if ((file_size >= video_size_75)); then
1 year ago
cleanup
fi
}
# Cleans up the leftovers
1 year ago
function cleanup() {
rm -vf "${video}" "${video}.json"
find "${d}" -size -1k -delete
1 year ago
}
for video; do
getinfo
[[ ${video_codec} =~ "av1" ]] && exit
[[ ${video_codec} =~ "hevc" ]] || convert_video_qsv
[[ ${video_codec} =~ "hevc" ]] && extract_subtitles
[[ ${video_codec} =~ "hevc" ]] && remux_video
2 years ago
done