#!/bin/bash ######################################################################## # This was SUPPOSED to be a quick hack to ffmpeg-convert videos, but # ffmpeg overheats my CPUs, pegging them at 95-100C, unless i run it # through cpulimit, leading to much more code. function die(){ local rc=$1 shift echo "Error: $@" 1>&2 exit $rc } [[ x = "x$1" ]] && { die 1 "Usage: $0 video_files..." } ffmpeg=$(which ffmpeg 2>/dev/null) [[ x = "${ffmpeg}x" ]] && { die 127 "cannot find ffmpeg in the PATH" } # Problem: ffmpeg is heating all 4 of my cores to 95-99C. # So... we'll throttle it with cpulimit. cpulimit=$(which cpulimit 2>/dev/null) [[ x = "${cpulimit}x" ]] && { die 127 "cannot find cpulimit in the PATH" } limitpct=150 # percent of CPU to limit to via $cpulimit. # ^^^^^^ 60% keeps my 4 cores all under 50C. It slows down conversion # by about 75-80%, though :/. # 75% provides better (not great) performance and all CPUs under 60C. # 85% keeps all CPUs under 60C. # 100% performs only marginally better than 60% and keeps the CPUs at # about 55-65C. # 200% keeps the CPUs between 70-75C (hot enough to engage my loud # fans). for file in "$@"; do [[ -e "$file" ]] || die 127 "file not found: $file" out="${file%.*}.mp4" [[ "$file" = "$out" ]] && die 126 "Input and output file would have the same name: $file" echo "=> Transcoding [$file] ==> [$out] ... " # reminder: ffmpeg -y option is necessary in conjunction with # cpulimit to work around some weird timing bug, where ffmpeg # reports $out as already existing. "${cpulimit}" -f -l ${limitpct} -- "$ffmpeg" -y -i "$file" "$out" || die $? "ffmpeg error code $?" touch --reference="$file" "$out" ls -lh "$file" "$out" echo done echo "Done!"