Last active
October 11, 2020 08:17
-
-
Save suminb/c4e30014d8777bfa4c822fd51a444a81 to your computer and use it in GitHub Desktop.
Concat mp4 files with ffmpeg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Author
suminb
commented
Oct 11, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment