FFmpeg:Example:HourMerge
시간 단위로 영상을 병합하고, 잘못된 재생 시간을 조정하는 방법.
파일 구조
rear
폴더 안에 년월일_시분초-마이크로초.mp4
파일 포맷으로 저장되어 있다:
rear/20240526_141021-580584.mp4
rear/20240526_141051-638931.mp4
rear/20240526_141503-609067.mp4
rear/20240526_143613-559271.mp4
rear/20240526_143927-544560.mp4
rear/20240526_144527-538071.mp4
rear/20240526_154527-295596.mp4
rear/20240526_162829-128770.mp4
rear/20240526_163207-123301.mp4
rear/20240526_164255-076005.mp4
rear/20240526_164901-076728.mp4
rear/20240526_165641-059543.mp4
rear/20240526_165831-051309.mp4
rear/20240526_170837-020066.mp4
rear/20240526_171051-009347.mp4
rear/20240526_173610-910914.mp4
rear/20240526_174504-871189.mp4
rear/20240526_175428-864039.mp4
rear/20240526_175614-821950.mp4
rear/20240526_180328-828448.mp4
rear/20240526_180430-846780.mp4
rear/20240526_181050-811778.mp4
rear/20240526_182504-735636.mp4
rear/20240526_182604-781367.mp4
rear/20240526_184500-685664.mp4
rear/20240526_184844-668940.mp4
rear/20240526_185656-601061.mp4
rear/20240526_191050-586440.mp4
rear/20240526_201050-417834.mp4
rear/20240526_211050-183465.mp4
rear/20240526_221049-964554.mp4
rear/20240526_231049-769453.mp4
...
Bash 스크립트
merge.sh
파일을 만든다:
#!/bin/bash
# Create an associative array to store files by their hour
declare -A files_by_hour
# Loop through each mp4 file in the current directory
for file in rear/*.mp4; do
# Extract the hour from the filename
hour="${file:0:16}"
# echo $hour
# Append the file to the array for the corresponding hour
files_by_hour["$hour"]+="$file "
done
# Loop through each hour in the array
for hour in "${!files_by_hour[@]}"; do
# Get the list of files for this hour
files=${files_by_hour[$hour]}
# Get the first file name to use as the base name for the output file
first_file=$(echo $files | awk '{print $1}')
# Create the output file name by extracting the base name from the first file
output_file="${first_file%.*}.mp4"
# Merge the files using ffmpeg
ffmpeg -f concat -safe 0 -i <(for f in $files; do echo "file '$PWD/$f'"; done) -fflags +genpts -c copy "rear-${output_file:5}"
# for f in $files; do
# echo " - $f"
# done
done
- 주어진 파일명 리스트를 시간 단위로 그룹화합니다.
- 각 그룹 내의 파일을 하나로 합칩니다.
- 합쳐진 파일을 해당 시간의 최초 파일명으로 저장합니다.