├── LICENSE ├── README.md └── auto-reencode /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013-2019 graysky 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Auto-reencode 2 | ### Purpose 3 | Mass convert lesser known formats such as asf, flv, and wmv to mp4 contained x264 files using ffmpeg. 4 | * Retains file's original bitrates (audio/video). 5 | * Retains file's original date/time stamp. 6 | * Temp files for 2 pass encode are written to /tmp (should be tmpfs). 7 | 8 | ### Usage 9 | Call the script in the directory containing the target files to convert. 10 | 11 | ### Links 12 | AUR Package: https://aur.archlinux.org/packages/auto-reencode 13 | 14 | ### Dependencies 15 | * bash 16 | * coreutils 17 | * ffmpeg 18 | * findutils 19 | -------------------------------------------------------------------------------- /auto-reencode: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Run in the dir containing the target files you wish to convert to x264. 3 | # There are no arguments. 4 | # The script will copy both the audio and video bitrates into the new file 5 | # via metadata from mediainfo and will retain the original DTS for you. 6 | 7 | export BLD="\e[01m" RED="\e[01;31m" BLU="\e[01;34m" NRM="\e[00m" 8 | 9 | command -v ffmpeg >/dev/null 2>&1 || { 10 | echo -e "${BLD}${RED}I require ffmpeg but it's not installed. Aborting.${NRM}" >&2 11 | exit 1; } 12 | 13 | command -v mediainfo >/dev/null 2>&1 || { 14 | echo "${BLD}${RED}I require mediainfo but it's not installed. Aborting.${NRM}" >&2 15 | exit 1; } 16 | 17 | pass1() { 18 | # For syntax, see: https://trac.ffmpeg.org/wiki/Encode/H.264 19 | nice -19 ffmpeg -nostdin -v fatal -stats -y -i "${file}" \ 20 | -c:v libx264 -preset slow -b:v "$vkbps"k -pass 1 \ 21 | -passlogfile "$logfile" \ 22 | -c:a aac -b:a "$akbps"k -f mp4 /dev/null 23 | } 24 | 25 | pass2() { 26 | # interactive mode of ffmpeg can fuck up without the -nostdin switch 27 | # https://www.igorkromin.net/index.php/2016/12/23/prevent-errors-during-ffmpeg-execution-in-a-loop/ 28 | nice -19 ffmpeg -nostdin -v fatal -stats -y -i "${file}" -c:v libx264 \ 29 | -preset slow -b:v "$vkbps"k -pass 2 \ 30 | -passlogfile "$logfile" \ 31 | -c:a aac -b:a "$akbps"k "${file%.*}.mp4" 32 | 33 | # remove the 1st pass log and mbtree files 34 | rm -f "$logfile"* 35 | } 36 | 37 | if df -T /tmp | grep -m 1 -q tmpfs; then 38 | # /tmp is tmpfs so use it for the mbtree and log files 39 | logfile=/tmp/auto-reencode_ffmpeg2pass 40 | else 41 | # /tmp is not tmpfs so just writeout mbtree and logs in the working dir 42 | logfile=auto-reencode_ffmpeg2pass 43 | fi 44 | 45 | echo 'Enter target file extension [flv,wmv,asf]:' 46 | read -r EXT 47 | 48 | # check for files to work on and exit if not are found 49 | if [[ -z $(find . ! -name . -prune -type f -name "*.$EXT" 2>/dev/null) ]]; then 50 | EXT="flv" 51 | if [[ -z $(find . ! -name . -prune -type f -name "*.$EXT" 2>/dev/null) ]]; then 52 | EXT="wmv" 53 | if [[ -z $(find . ! -name . -prune -type f -name "*.$EXT" 2>/dev/null) ]]; then 54 | EXT="asf" 55 | if [[ -z $(find . ! -name . -prune -type f -name "*.$EXT" 2>/dev/null) ]]; then 56 | echo -e "${BLD}Cannot find any target files so no work to do.${NRM}" 57 | echo -e "${BLD}Define a target file type in this dirtree and try again.${NRM}" 58 | exit 1 59 | fi 60 | fi 61 | fi 62 | fi 63 | 64 | [[ -z "$EXT" ]] && EXT="flv" 65 | 66 | total=$(find . ! -name . -prune -type f -name "*.$EXT"|wc -l) 67 | 68 | while IFS= read -r -d '' file; do 69 | (( n++ )) 70 | # find bitrate for video and audio 71 | _vkbps=$(mediainfo --inform="Video;%BitRate%" "$file") 72 | _akbps=$(mediainfo --inform="Audio;%BitRate%" "$file") 73 | ptime=$(mediainfo --inform="General;%Duration/String2%" "$file") 74 | 75 | ### TODO make this shorter code perhaps using an array check function 76 | 77 | echo -e "${BLD}${RED}Task $n of $total${NRM}" 78 | echo -e "${BLD} file to encode :${BLU} $file" 79 | 80 | # audio and video bitrate must be an integer 81 | if [[ "$_vkbps" =~ ^-?[0-9]+$ ]]; then 82 | vkbps=$((_vkbps / 1000)) 83 | else 84 | flag=1 85 | fi 86 | 87 | if [[ "$_akbps" =~ ^-?[0-9]+$ ]]; then 88 | akbps=$((_akbps / 1000)) 89 | else 90 | akbps=0 91 | fi 92 | 93 | echo -e "${NRM}${BLD} video bitrate : $vkbps kbps${NRM}" 94 | echo -e "${BLD} audio bitrate : $akbps kbps${NRM}" 95 | echo -e "${BLD} play time : $ptime${NRM}" 96 | 97 | # do not overwrite existing files 98 | if [[ -e "${file%.*}.mp4" ]]; then 99 | echo >&2 Output file already exists: "${file%.*}.mp4" 100 | echo >&2 Skipping... 101 | echo >&2 102 | continue 103 | fi 104 | 105 | if [[ $flag -eq 1 ]]; then 106 | echo -e "${BLD} ==> Skipping $file due to bad audio or video bitrate${NRM}." 107 | else 108 | # encode 109 | echo -e "${BLD} pass 1 of 2 stats : ${NRM}" 110 | 111 | if pass1; then 112 | echo -e "${BLD} pass 2 of 2 stats : ${NRM}" 113 | pass2 114 | else 115 | echo -e "${BLD}${RED} Pass 1 failed, exiting!${NRM}" 116 | exit 1 117 | fi 118 | 119 | # restore dts 120 | touch -r "$file" "${file%.*}.mp4" 121 | fi 122 | # reset badflag to null 123 | unset flag 124 | echo 125 | done < <(find . ! -name . -prune -type f -name "*.$EXT" -print0) 126 | 127 | # vim:set ts=2 sw=2 et: 128 | --------------------------------------------------------------------------------