-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert.sh
More file actions
executable file
·174 lines (164 loc) · 4.73 KB
/
convert.sh
File metadata and controls
executable file
·174 lines (164 loc) · 4.73 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash
set -u
# Global values.
A1_GLOBAL_VIDEO_API=v4l2
A1_GLOBAL_AUDIO_API=alsa
A1_GLOBAL_VIDEO_CODEC=h264
A1_GLOBAL_AUDIO_CODEC=aac
A1_GLOBAL_PIXEL_FMT=yuv420p
# Defined variables.
output_file=
video_input=
audio_input=
tape_standard=auto
stop_time=
language=en
# Help message.
show_usage() {
cat << EOF
$(basename $0) [OPTIONS]
Capture and encode VHS video and audio streams.
OPTIONS:
-o output file name
-v video input (/dev/video*)
-a audio input (hw:*,0)
-t recording duration ([HH:]MM:SS)
-s standard PAL or NTSC
-l language code (default: en)
EOF
exit 0
}
# Read script arguments.
while getopts ':ho:v:a:t:s:l:' OPTION; do
case "$OPTION" in
o) output_file="$OPTARG";;
v) video_input="$OPTARG";;
a) audio_input="$OPTARG";;
t) stop_time="$OPTARG";;
s) tape_standard="$OPTARG";;
l) language="$OPTARG";;
*) show_usage;;
esac
done
# Current directory.
source="$(realpath "${BASH_SOURCE[0]}" 2> /dev/null)"
current_dir="${source%/*}"
[[ -z "$current_dir" || ! -d "$current_dir" ]] && current_dir="$PWD"
# Include general functions.
source "$current_dir/scripts/helper.sh" || exit 1
source "$current_dir/scripts/message.sh" || exit 1
source "$current_dir/scripts/validator.sh" || exit 1
if ! cmd_exists "ffmpeg"; then
# Check dependencies.
print_msg "ffmpeg_missing" "$language"
exit 1
elif [ $UID -eq 0 ]; then
# Check execution privilages.
print_msg "root_exec" "$language"
exit 1
fi
# Validate script arguments.
declare -a validation_errors
validation_errors+=($(validate_output_file_name "$output_file"))
validation_errors+=($(validate_video_grabbing_device "$video_input"))
validation_errors+=($(validate_audio_grabbing_device "$audio_input"))
validation_errors+=($(validate_timestamp "$stop_time"))
validation_errors+=($(validate_standard_name "$tape_standard"))
if [ ${#validation_errors[@]} -ne 0 ]; then
for verror in ${validation_errors[@]}; do
print_msg "$verror" "$language"
done
exit 1
fi
unset validation_errors verror
# Form part of video input options.
# Args:
# $1 video device
get_video_input_options() {
local input="$1"
local api=${A1_GLOBAL_VIDEO_API:-}
local cmd_part=''
if [ -n "$input" ]; then
[ -n "$api" ] && cmd_part+="-f $api "
cmd_part+="-thread_queue_size 512 -i $input"
fi
printf -- "$cmd_part"
}
# Form part of audio input options.
# Args:
# $1 audio device
get_audio_input_options() {
local input="$1"
local api=${A1_GLOBAL_AUDIO_API:-}
local cmd_part=''
if [ -n "$input" ]; then
[ -n "$api" ] && cmd_part+="-f $api "
cmd_part+="-thread_queue_size 512 -i $input"
fi
printf -- "$cmd_part"
}
# Form part of video output options.
# Args:
# $1 video device
get_video_output_options() {
local input="$1"
local color=${A1_GLOBAL_PIXEL_FMT:-}
local codec=${A1_GLOBAL_VIDEO_CODEC:-}
local cmd_part=''
if [ -n "$input" ]; then
[ -n "$color" ] && cmd_part+=" -pix_fmt $color"
[ -n "$codec" ] && cmd_part+=" -c:v $codec"
[ "$codec" = h264 ] && cmd_part+=" -preset veryfast -crf 25"
fi
printf -- "$cmd_part"
}
# Form part of audio output options.
# Args:
# $1 audio device
get_audio_output_options() {
local input="$1"
local codec=${A1_GLOBAL_AUDIO_CODEC:-}
local cmd_part=''
if [ -n "$input" ]; then
[ -n "$codec" ] && cmd_part+=" -c:a $codec"
fi
printf -- "$cmd_part"
}
# Form part of encode options for specifiec signal standard.
# Args:
# $1 signal standard
# $2 video device
get_standard_output_options() {
local video="$2"
local standard="$1"
local cmd_part=''
if [[ -n "$video" && -n "$standard" ]]; then
[ "${standard^^}" = PAL ] && cmd_part+=" -s 720x576 -r 25 -aspect 4:3"
[ "${standard^^}" = NTSC ] && cmd_part+=" -s 720x580 -r 29.97 -aspect 4:3"
fi
printf -- "$cmd_part"
}
# Form part of time limit options.
# Args:
# $1 recording time
get_time_output_options() {
local time="$1"
local cmd_part=''
if [ -n "$time" ]; then
cmd_part+=" -t $time"
fi
printf -- "$cmd_part"
}
# Assemble ffmpeg command.
ffmpeg_command=$(printf -- "ffmpeg -loglevel 16 %s %s %s %s %s %s -y %s" \
"$(get_video_input_options "$video_input")" \
"$(get_audio_input_options "$audio_input")" \
"$(get_video_output_options "$video_input")" \
"$(get_standard_output_options "$tape_standard" "$video_input")" \
"$(get_audio_output_options "$audio_input")" \
"$(get_time_output_options "$stop_time")" \
"$(printf %q "$output_file")" \
)
printf "\n%s\n" "$(get_msg "capture_started" "$language")"
echo "$ffmpeg_command"
bash -c "$ffmpeg_command"