转换为 MP4 脚本
#!/bin/bash
NAME="."
EXT=".rmvb"
if [ "${2}x" != ""x ]; then
EXT="${2}"
fi
if [ "${1}x" == ""x ]; then
NAME="."
else
NAME="${1}"
fi
if [ "${NAME}x" = "."x ]; then
IFS=$'\n'
for i in `ls *${EXT}`
do
newfname=$(echo $i | sed "s/$EXT/\.mp4/")
#ffmpeg -i "${i}" -c:v h264 -c:a mp3 "${newfname}"
ffmpeg -i "${i}" -c:v libx265 -c:a mp3 "${newfname}"
done
else
newfname=$(echo $NAME | sed "s/$EXT/\.mp4/")
ffmpeg -i "${NAME}" -c:a aac -c:v libx265 "${newfname}"
fi
-c:a mp3
mp3 可修改为 aac
等音频格式码。或直接 copy
复制流,不转音频编码。
-c:v libx265
libx265 可修改为 h264、libx264 (H.264 格式)和 libx265 (H.265 格式),其中 H.265 占用资源比 H。264 少一倍。
若将 -c:a mp3
修改为 -an
则禁止转换音频。
若将 -c:v libx265
修改为 -vn
则禁止转换视频。
手动设置IFS排除空格分隔符
在 linux 中内置分隔符IFS (Internal Field Seperator) 默认为空格、制表符、换行符
查看默认分隔符:
set | grep ^IFS
$ IFS=$' \t\n'
优化后
#!/bin/bash
NAME="."
EXT=".rmvb"
if [ "${2}x" != ""x ]; then
EXT="${2}"
fi
if [ "${1}x" == ""x ]; then
NAME="."
else
NAME="${1}"
fi
if [ "${NAME}x" = "."x ]; then
IFS=$'\n'
for i in `ls "*${EXT}"`
do
echo -e "\n\n\e[0;31m${i} ...\e[m \n"
newfname=$(echo "${i}" | sed "s/$EXT/\.mp4/")
ffmpeg -i "${i}" -max_muxing_queue_size 1024 -c:a copy -c:v libx265 -y "${newfname}"
done
else
newfname=$(echo "${NAME}" | sed "s/$EXT/\.mp4/")
ffmpeg -i "${NAME}" -max_muxing_queue_size 1024 -c:a copy -c:v libx265 -y "${newfname}"
fi
将本目录及子目录下指定的格式视频转换成 mp4
#!/bin/bash
tomp4() {
# LIST=$(find ./ -type f \( -name "*.avi" -o -name "*.wmv" -o -name "*.mp4" -o -name "*.mpg" -o -name "*.3gp" \))
EXT="${1}"
echo -e "\n\nFROM: ${EXT} to mp4\n"
IFS=$'\n'
for name in $(find ./ -name "*${EXT}")
do
echo -e "\n\e[0;31mfile name: << ${name} >>\e[m \n"
newfname=$(echo "${name}" | sed "s/${EXT}/.mp4/")
ffmpeg.exe -i "${name}" -max_muxing_queue_size 1024 -c:a copy -c:v hevc_amf -y "${newfname}"
#echo "${newfname}"
done
}
main() {
exts=".avi .AVI .wmv .WMV .rmvb .RMVB .rm .RM .mpg .MPG .mpeg .MPEG .3gp .3GP"
array=(${exts//,/ })
for ext in ${array[@]}
do
tomp4 "${ext}"
done
}
main "@$" || exit 1
Windows 批处理
@echo off
setlocal
set exts=.avi .wmv .rm .rmvb .mpg .mpeg .3gp
call :parse "%exts%"
goto :eos
:: for ext
:parse
set list=%1
set list=%list:"=%
FOR /f "tokens=1* delims= " %%a IN ("%list%") DO (
if not "%%a" == "" call :files %%a
if not "%%b" == "" call :parse "%%b"
)
goto :eos
:: tomp4
:files
set EXT=%1
echo.FROM: %EXT% to mp4
:: for /r . %%i in (*%EXT%) do echo %%i
for /f "tokens=1* delims=" %%a in ('dir /B /S *%EXT%') do (
if not "%%a" == "" call :tomp4 "%%a" %EXT%
if not "%%b" == "" call :files "%%b"
)
goto :eos
:: to mp4
:tomp4
setlocal enabledelayedexpansion
set filename=%1
set newfname=%1
set substr=%2
set replace=.mp4
call set "newfname=%%newfname:!substr!=!replace!%%"
ffmpeg.exe -i %filename% -max_muxing_queue_size 1024 -qp 0 -c:a aac -c:v hevc_amf -y !newfname!
goto :eos
:eos
endlocal