Last active
December 4, 2019 13:07
-
-
Save vdebergue/4e0596701800530e4208ccf61124d031 to your computer and use it in GitHub Desktop.
Transcode h264 to h265
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
set -x | |
set -e | |
FOLDER="$1" | |
MAX_BITRATE=3000000 | |
if [ ! -d "$FOLDER" ]; then | |
echo "Please input a folder" | |
exit 1 | |
fi | |
transcode () { | |
local input_file="$1" | |
local output_file="$2" | |
local tmp_file="tmp-$output_file" | |
nice -n 10 ffmpeg -y -i "$FOLDER/$input_file" -map 0 -c copy -c:v libx265 -x265-params crf=28 "$FOLDER/$tmp_file" | |
result=$? | |
if [ $result -ne 0 ]; then | |
echo "Error during process" | |
rm -rf "$FOLDER/$tmp_file" | |
exit 2 | |
fi | |
rm -f "$FOLDER/$input_file" | |
mv "$FOLDER/$tmp_file" "$FOLDER/$output_file" | |
} | |
SAVEIFS=$IFS | |
IFS=$(echo -en "\n\b") | |
VIDEO_FILES=$(ls -1 "$FOLDER" | grep -E '.avi$|mkv$|mp4$') | |
H264_FILES=() | |
for file in $VIDEO_FILES; do | |
codec=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$FOLDER/$file") | |
if [ "$codec" = "h264" ]; then | |
H264_FILES+=("$file") | |
elif [ "$codec" = "mpeg4" ]; then | |
H264_FILES+=("$file") | |
elif [ "$codec" = "hevc" ]; then | |
bit_rate=$(ffprobe -v error -select_streams v:0 -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1 "$FOLDER/$file") | |
if [ "$bit_rate" -gt "$MAX_BITRATE" ]; then | |
echo "transcode h265 file $file bitrate=$bit_rate" | |
H264_FILES+=("$file") | |
fi | |
fi | |
done | |
IFS=$SAVEIFS | |
ORIGINAL_SIZE=$(du -sh "$FOLDER" | awk '{print $1}') | |
echo "Transcoding files: ${H264_FILES[@]}" | |
for input_file in "${H264_FILES[@]}"; do | |
echo "Taking file $input_file" | |
output_file=${input_file//264/265} | |
if [[ "$output_file" != *"265"* ]]; then | |
output_file=$(sed -e "s/\.mkv/\.x265\.mkv/; s/\.avi/\.x265.mkv/; s/\.mp4/\.x265.mkv/;" <<< "$input_file") | |
fi | |
#if [ ! -f "$FOLDER/$output_file" ]; then | |
# transcode "$input_file" "$output_file" | |
#fi | |
transcode "$input_file" "$output_file" | |
done | |
FINAL_SIZE=$(du -sh "$FOLDER" | awk '{print $1}') | |
echo "Transcoding reduced folder from $ORIGINAL_SIZE to $FINAL_SIZE" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment