Skip to content

Instantly share code, notes, and snippets.

@sega-yarkin
Last active February 20, 2021 01:00
Show Gist options
  • Save sega-yarkin/0f98fffe5613049981473176c83e874f to your computer and use it in GitHub Desktop.
Save sega-yarkin/0f98fffe5613049981473176c83e874f to your computer and use it in GitHub Desktop.
ffmpeg video convert from mts format
#!/bin/sh
# for Macbook (MacOS)
# prereq.:
# - custom ffmpeg installation:
# brew tap justinmayer/tap
# brew install justinmayer/tap/ffmpeg --with-fdk-aac --with-libvidstab --with-webp
# - brew install exiftool
function mts_to_mp4_h265_cpu() {
mts_file="$1"
vbitrate="${2:-15000k}"
preset="${3:-medium}" # ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow
abitrate="${4:-256k}"
mp4_file="${mts_file%.*}.mp4"
ffmpeg -hide_banner -y -i "$mts_file" -vf yadif -c:v libx265 -b:v "$vbitrate" -preset "$preset" -x265-params pass=1 -an -f mp4 /dev/null
ffmpeg -hide_banner -i "$mts_file" -vf yadif -c:v libx265 -b:v "$vbitrate" -preset "$preset" -x265-params pass=2 -tag:v hvc1 -c:a aac -b:a "$abitrate" "$mp4_file"
exiftool -overwrite_original -TagsFromFile "$mts_file" "$mp4_file"
touch -r "$mts_file" "$mp4_file"
}
function mts_to_mp4_h265_gpu() {
mts_file="$1"
vbitrate="${2:-15000k}"
abitrate="${3:-256k}"
mp4_file="${mts_file%.*}.mp4"
ffmpeg -hide_banner -y -i "$mts_file" -vf yadif -c:v hevc_videotoolbox -b:v "$vbitrate" -f mp4 -tag:v hvc1 -c:a aac -b:a "$abitrate" "$mp4_file"
exiftool -overwrite_original -TagsFromFile "$mts_file" "$mp4_file"
touch -r "$mts_file" "$mp4_file"
}
function mts_to_mp4_h265_gpu_vidstab() {
mts_file="$1"
bitrate="${2:-15000k}"
shakiness="${3:-5}"
smoothing="${4:-10}"
zoom="${5:-0}"
mp4_file="${mts_file%.*}.mp4"
stab_stat="transforms.trf"
detect_opts="shakiness=${shakiness}:result='${stab_stat}'"
transform_opts="smoothing=${smoothing}:interpol=bicubic:zoom=${zoom}:input='${stab_stat}'"
ffmpeg -hide_banner -i "$mts_file" -vf "vidstabdetect=${detect_opts}" -f null -
ffmpeg -hide_banner -y -i "$mts_file" -vf "yadif, vidstabtransform=${transform_opts}, unsharp=5:5:0.8:3:3:0.4" \
-c:v hevc_videotoolbox -b:v "$bitrate" -f mp4 -tag:v hvc1 -c:a aac -b:a 256k "$mp4_file"
exiftool -overwrite_original -TagsFromFile "$mts_file" "$mp4_file"
touch -r "$mts_file" "$mp4_file"
rm "$stab_stat"
}
function convert_all() {
# Convert all MTS files in current directory
where="${1:-gpu}"
for mts_file in *.MTS; do
mts_to_mp4_h265_${where} "$mts_file"
done
rm -f x265_2pass.* || :
}
function for_tg_mp4_720_gpu() {
ifile="$1"
bitrate="${2:-3000k}"
ofile="${ifile%.*}_720.mp4"
ffmpeg -hide_banner -y -i "$ifile" -vf scale=-1:720 -c:v hevc_videotoolbox -b:v "$bitrate" -f mp4 -tag:v hvc1 -c:a aac -b:a 128k "$ofile"
touch -r "$ifile" "$ofile"
}
$ffmpeg = "C:\Program Files\ffmpeg\bin\ffmpeg.exe"
$exiftool = "C:\Program Files\exiftool.exe"
. $exiftool -d '%Y%m%d_%H%M%S' '-Filename<%f_${DateTimeOriginal}.%le' -ext mts .
Get-ChildItem ".\*.MTS" | % {
$InName = $_.Name
$OutName = ([io.fileinfo]$InName).basename + ".mp4"
. $ffmpeg -hide_banner -y -i $InName -vf yadif -c:v libx265 -b:v 1500K -preset medium -x265-params pass=1 -an -f mp4 NUL
. $ffmpeg -hide_banner -i $InName -vf yadif -c:v libx265 -b:v 1500K -preset medium -x265-params pass=2 -tag:v hvc1 -c:a aac -b:a 256k $OutName
. $exiftool -overwrite_original -TagsFromFile $InName $OutName
$OutMeta = Get-Item $OutName
$OutMeta.CreationTime = $_.CreationTime
$OutMeta.LastWriteTime = $_.LastWriteTime
$OutMeta.LastAccessTime = $_.LastAccessTime
}
Remove-Item ".\ffmpeg2pass*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment