Skip to content

Instantly share code, notes, and snippets.

@suminb
Last active October 11, 2020 08:17
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 suminb/c4e30014d8777bfa4c822fd51a444a81 to your computer and use it in GitHub Desktop.
Save suminb/c4e30014d8777bfa4c822fd51a444a81 to your computer and use it in GitHub Desktop.
Concat mp4 files with ffmpeg
#!/bin/bash
#ffmpeg -f concat -i <(find . -name '*.mp4' -printf "file '%p'\n") -c copy output.mp4
#ffmpeg -f concat -i <(printf "file '$(pwd)/%s'\n" *.mp4) -c copy output.mp4
#ffmpeg -f concat -i <(for f in ./*.mp4; do echo "file '$f'"; done) -c copy output.mp4
list_file="list.txt"
archive_path="processed"
function get_unique_dates {
pattern="20*.mp4"
(for file in $pattern; do
date=${file:0:8}
hour=${file:9:2}
printf '%s_%s\n' $date $hour
done) | uniq
}
function generate_file_list {
list_file=$1
datehour=$2 # e.g., 20170905_17
suffix=$3 # F: front, R: rear
(for file in $(find . -name "${datehour}*${suffix}.mp4"); do
echo "file $file";
done) > $list_file
}
function concat_media_files {
list_file=$1
output_file=$2
datehour=$3
date=${datehour:0:6}
hour=${datehour:7:2}
ffmpeg -f concat -safe 0 -i $list_file \
-metadata ICRD="$date $hour:00:00" \
-map_metadata 0:g \
-c copy "$output_file" -y
# NOTE: It seems ffmpeg does not actually map the metadata properly,
# so we are overriding the file modification date
if [[ -f $output_file ]]; then
touch -t $(printf '%s%s00' $date $hour) "$output_file"
fi
}
function archive_processed_files {
list_file=$1
archive_path=$2
while read line; do
set $line
mv $2 $archive_path/
done < $list_file
}
mkdir $archive_path
for datehour in $(get_unique_dates); do
generate_file_list "$list_file" $datehour "F"
output="Dashboard Cam Front $datehour.mp4"
concat_media_files "$list_file" "$output" $datehour
archive_processed_files "$list_file" "$archive_path"
generate_file_list "$list_file" $datehour "R"
output="Dashboard Cam Rear $datehour.mp4"
concat_media_files "$list_file" "$output" $datehour
archive_processed_files "$list_file" "$archive_path"
done
@suminb
Copy link
Author

suminb commented Oct 11, 2020

ffmpeg -f concat -safe 0 -i <(for f in *R.MP4; do echo "file `pwd`/'$f'"; done) -c copy Rear.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment