-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtgm-playback
executable file
·103 lines (97 loc) · 3.06 KB
/
tgm-playback
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
mameroot=${MAMEROOT:-"$HOME/.mame"}
if [ ! "$1" ]; then
echo "Usage: `basename "$0"` game [tag]
MAME root directory: $mameroot" 1>&2
exit 1
fi
game="$1"
shift
launcher="`which tgm-launcher`"
[ -f "${launcher:="`dirname "$0"`/tgm-launcher"}" ] \
|| launcher="`dirname "\`readlink -f "$0"\`"`/tgm-launcher"
[ ! -f "$launcher" ] && echo 'Unable to find tgm-launcher helper script.' 1>&2 \
|| . "$launcher"
nv="$mameroot/nvram/$game.nv"
inp="$mameroot/inp/$game"
snap="$mameroot/snap"
meta="$mameroot/meta.$game"
cd "$meta" &>/dev/null
echo 'Choose an input file:'
if [ ! "$1" ]; then
ls
read -ep '> ' name
else
name="$1"
fi
inpfile="$inp.$name.inp"
nvfile="$nv.$name"
metafile="$meta/$name"
if [ ! -f "$inpfile" ]; then
echo 'Input file not found.'
exit 1
fi
if [ ! -f "$nvfile" ]; then
echo 'Matching NVRAM not found. Playback is not possible.'
exit 1
fi
if [ ! -r "$metafile" ]; then
echo 'Playback metadata not found. You will have to manually stop playback.'
seconds=''
else
read seconds < "$metafile"
if [ ! "$seconds" ]; then
echo 'Recording length unknown. You will have to manually stop playback.'
seconds=''
else
echo "Recording is $seconds seconds long."
seconds="-seconds_to_run $((seconds+1))"
fi
fi
cp "$nvfile" "$snap/$game.nv"
echo -n 'Current directory: '
cd -
out=''
read -ep 'Give an output filename if you want to encode: ' -i "$game.$name.mkv" out
while [ "$out"x != x -a -e "$out" ]; do
read -ep "'$out' already exists. Confirm the filename: " -i "$out" out2
[ "$out2" = "$out" ] && rm -f "$out" && break
out="$out2"
done
if [ "$out" ]; then
ac=''
if [ "$game" = 'tgm2p' ]; then
echo "Choose a crop mode:
1 Auto-crop to P1's last game
2 Auto-crop to P2's last game
Anything else: no auto-crop."
read -p ' > ' ac
fi
echo "Encoding '$name' to '$out'"
avi="$out.avi"
avifile="$snap/$avi"
trap "rm -f '$avifile' '$snap/$game.nv'" SIGINT
$game $seconds -mt -nothrottle -nosleep -window -resolution 320x240 -nvram_directory 'snap' -playback "$game.$name.inp" -aviwrite "$avi" 2>/dev/null
st=0
if [ "$ac" ] && [ "$ac" = 1 -o "$ac" = 2 ]; then
echo "Scanning video stream"
[ $ac = 1 ] && crop=80:150:18:50
[ $ac = 2 ] && crop=80:150:178:50
st=`mplayer -quiet -benchmark -noconfig all -nosound -vo null -vf flip,crop=$crop,blackframe=100:50 "$avifile" | \
awk 'BEGIN {l=0; ll=0} /^vf_blackframe/ {n=int($2); if (l!=0 && n-l > 600) ll=l; l=n} END {print ll/60}'`
[ ! "$st" ] && st=0
echo "Starting encode $st seconds in"
fi
ffmpeg -i "$avifile" -ss $st -acodec libfaac -ab 96k -vcodec libx264 -vpre normal -crf 20 -threads 0 "$out"
success=$?
rm -f "$avifile" "$snap/$game.nv"
if [ $success = 0 ]; then
echo "Done encoding '$out'."
else
echo "Error encoding '$out'."
exit $success
fi
else
$game $seconds -nvram_directory 'snap' -playback "$game.$name.inp" 2>/dev/null
rm "$snap/$game.nv"
fi