Skip to content

Instantly share code, notes, and snippets.

@miry
Last active December 19, 2022 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miry/02ce08a78a217c887e103897c7759eed to your computer and use it in GitHub Desktop.
Save miry/02ce08a78a217c887e103897c7759eed to your computer and use it in GitHub Desktop.
Automate compressing of video files from GoPro
#!/usr/bin/env bash
# USAGE: concat.sh files...
# curl https://gist.githubusercontent.com/miry/02ce08a78a217c887e103897c7759eed/raw/concat.sh | bash -s -- files...
# Reerences: https://trac.ffmpeg.org/wiki/Concatenate
#
# Example:
# curl https://gist.githubusercontent.com/miry/02ce08a78a217c887e103897c7759eed/raw/concat.sh | bash -s -- GH0*0172.MP4
set -euxo pipefail
files=$@
: Create a file config for ffmpeg
listfile="_concat.txt"
[ -f ${listfile} ] && rm ${listfile}
lastfile=""
for file in $files ; do
if [ ! -e "$file" ]; then
echo "Video files not found"
exit
fi
echo "file '${file}'" >> "${listfile}"
lastfile="${file}"
done
if [ -z "${lastfile}" ]; then
echo "No video files provided"
exit
fi
name=$(basename "${lastfile%.*}")
output="${name}_concat.MP4"
ffmpeg -safe 0 -nostdin -f concat -i "${listfile}" -c copy "${output}"
touch -r "${lastfile}" "${output}"
#!/usr/bin/env bash
# USAGE: conver.sh [codec]
# curl https://gist.githubusercontent.com/miry/02ce08a78a217c887e103897c7759eed/raw/convert.sh | bash -s -- libx265
set -euxo pipefail
codec=${1:-libx264}
: Remove thumbnails files
[ -f *.THM ] && rm *.THM
[ -f *.LRV ] && rm *.LRV
: Ensure there is an output folder
output_dir="processed"
mkdir -p "${output_dir}"
for file in *.MP4 ; do
if [ ! -e "$file" ]; then
echo "Video files not found"
break
fi
output="${output_dir}/${file}"
if [ -f "$output" ]; then
echo "skipping. $output exists."
continue
fi
ffmpeg -nostdin -i $file -threads 0 -vcodec $codec $output
touch -r $file $output
done
Dir["*.MP4"].each do |f|
puts "Process: #{f}"
if f.include?("_concat.") || f.include?("_metadata.mp4")
puts " > Skip, the filename: #{f}"
next
end
fs = File::Stat.new(f)
`ffmpeg -dump -i "#{f}"`
new_time = fs.ctime.utc.strftime("%FT%T.%L000Z")
output = File.basename(f)
puts output
`ffmpeg -safe 0 -i "#{f}" -c copy -movflags use_metadata_tags -metadata creation_time="#{new_time}" "#{output}"`
FileUtils.touch "#{output}", mtime: fs.ctime
end
require 'fileutils'
# GOPRO uses wrong date and time. I identified close time +- 4 hours.
# Update ctime, atime and mtime for all files.
# Skip files that were created manually.
# Difference real time and when it was created
diff=208372731
Dir["*.{MP4,JPG}"].each do |f|
puts "Process: #{f}"
if f.include?("_concat.")
puts " > Skip, the filename concat: #{f}"
next
end
fs = File::Stat.new(f)
p fs
if fs.mtime.year < 2022
new_time = fs.mtime + diff
puts new_time
# TODO: Uncomment next line after checking
# FileUtils.touch f, mtime: new_time
else
puts " > Skip, the mtime: #{fs.mtime}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment