Skip to content

Instantly share code, notes, and snippets.

@jwoglom
Created June 3, 2022 03:04
Show Gist options
  • Save jwoglom/88060db9fc1de82edb498342d5e7f280 to your computer and use it in GitHub Desktop.
Save jwoglom/88060db9fc1de82edb498342d5e7f280 to your computer and use it in GitHub Desktop.
Convert AVCHD folder containing BDMV MTS video files to MP4 with ffmpeg
#!/bin/bash -x
# Invoke with:
# ./avchd-mts-to-mp4.sh AVCHD/BDMV/STREAM/*.MTS
exifdateformat='%Y:%m:%d %T%z'
ffmpegdateformat='%FT%T'
filenamedateformat='%Y%m%d_%H%M%S'
getfileinfodateformat='%m/%d/%Y %T'
for srcfile in "$@"
do
exifcreatetime=$(exiftool -s -s -s -datetimeoriginal $srcfile)
echo "Get date/time:$exifcreatetime"
if [[ "$exifcreatetime" == "" ]]; then
ffmpegcreatetime=$(date -j -f "$getfileinfodateformat" "$(GetFileInfo -d $srcfile)" +"$ffmpegdateformat")
echo "ffmpeg creation time:$ffmpegcreatetime"
filecreatetime=$(date -j -f "$getfileinfodateformat" "$(GetFileInfo -d $srcfile)" +"$filenamedateformat")
echo "Filecreatetime: $filecreatetime"
else
exifcreatenodash=$(python -c "a='$exifcreatetime';print(a[:-3]+a[-2]+a[-1])")
echo "Exifcreatenodash: $exifcreatenodash"
ffmpegcreatetime=$(date -j -f "$exifdateformat" "$exifcreatenodash" +"$ffmpegdateformat")
echo "transformed date/time:$ffmpegcreatetime"
filecreatetime=$(date -j -f "$exifdateformat" "$exifcreatenodash" +"$filenamedateformat")
fi
echo "filename date/time:$filecreatetime"
echo "Get value:$srcfile"
ffmpeg -i $srcfile -c copy -metadata creation_time=${ffmpegcreatetime}Z ${filecreatetime}.mp4
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment