Repository navigation
Expand file tree
/
Copy pathconvert.sh
More file actions
executable file
·82 lines (66 loc) · 2.16 KB
/
Copy pathconvert.sh
File metadata and controls
executable file
·82 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
set -euo pipefail
if [ $# -lt 1 ] || [ ! -d "$1" ]; then
cat >&2 <<'EOF'
Usage: convert.sh <directory-with-mp4s>
Recursively converts dated .mp4 recording segments under <dir> to numbered .mov
for DaVinci Resolve on Linux (video stream-copied, audio re-encoded to 16-bit
PCM 48kHz stereo so Resolve can play the audio).
Each session folder's segments become mov/01.mov .. mov/NN.mov, sorted by
filename date ascending (lowest date = 01). Files without a date in the name
are skipped. Original .mp4 files are left untouched.
EOF
exit 1
fi
src_dir="$1"
dated_re='.*/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}-[0-9]{2}-[0-9]{2}\.mp4'
# Unique folders containing dated mp4 segments, sorted.
mapfile -d '' folders < <(find "$src_dir" -type f -regextype posix-extended -iregex "$dated_re" -printf '%h\0' | sort -zu)
if [ ${#folders[@]} -eq 0 ]; then
echo "No dated .mp4 segments (YYYY-MM-DD HH-MM-SS.mp4) found under: $src_dir"
exit 0
fi
converted=0
skipped=0
failed=0
for folder in "${folders[@]}"; do
# Dated mp4s directly in this folder, sorted (name sort == chronological for OBS).
mapfile -d '' files < <(find "$folder" -maxdepth 1 -type f -regextype posix-extended -iregex "$dated_re" -print0 | sort -z)
N=${#files[@]}
if [ "$N" -eq 0 ]; then
continue
fi
n_str="$N"
width=${#n_str}
if [ "$width" -lt 2 ]; then
width=2
fi
if [ "$N" -gt 1 ]; then s="s"; else s=""; fi
echo "==> $folder ($N segment$s)"
i=0
for f in "${files[@]}"; do
i=$((i + 1))
out_name="$(printf "%0${width}d" "$i")"
out="$folder/mov/$out_name.mov"
base="$(basename "$f")"
if [ -f "$out" ]; then
echo " [$i/$N] skipped (exists): $out_name.mov <- $base"
skipped=$((skipped + 1))
continue
fi
mkdir -p "$folder/mov"
echo " [$i/$N] converting: $base -> $out_name.mov"
if ffmpeg -y -loglevel error -i "$f" \
-c:v copy \
-c:a pcm_s16le -ar 48000 -ac 2 \
"$out"; then
converted=$((converted + 1))
else
echo " ! FAILED: $f" >&2
rm -f "$out"
failed=$((failed + 1))
fi
done
done
echo ""
echo "Done: $converted converted, $skipped skipped, $failed failed."