-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpeg.sh
More file actions
executable file
·218 lines (198 loc) · 11.2 KB
/
ffmpeg.sh
File metadata and controls
executable file
·218 lines (198 loc) · 11.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env bash
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/ffmpeg.sh
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/ffmpeg.sh
## https://davoudarsalani.ir
source ~/main/scripts/helps.sh
source ~/main/scripts/utils.sh
title="${0##*/}"
function prompt {
for _ in "$@"; {
case "$1" in
-i )
input_file="${input_file:-"$(get_input 'input file')"}" ;;
-x )
offset="${offset:-"$(get_input 'offset (e.g. 1.2 or -0.8)')"}" ;;
-o )
output="${output:-"$(get_input 'output file')"}" ;;
-s )
start="${start:-"$(get_input 'start time (e.g. 00:00:09)')"}" ;;
-e )
end="${end:-"$(get_input 'end time (e.g. 00:18:34)')"}" ;;
-a )
audio_input="${audio_input:-"$(get_input 'audio input file')"}" ;;
-v )
video_input="${video_input:-"$(get_input 'video input file')"}" ;;
-t )
time="${time:-"$(get_input 'time (e.g. 00:00:09)')"}" ;;
-u )
audio_stream="${audio_stream:-"$(get_input 'audio stream to keep (e.g. 1)')"}" ;;
-c )
if [ ! "$camera" ]; then
readarray -t cameras < <(find /dev/ -mindepth 1 -maxdepth 1 -iname '*video*' | sort)
fzf__title=''
camera="$(pipe_to_fzf "${cameras[@]}")" && wrap_fzf_choice "$camera" || exit 37
fi
;;
-f )
sub_file="${sub_file:-"$(get_input 'subtitle file')"}" ;;
-d )
sub_index="${sub_index:-"$(get_input 'subtitle index (e.g. 0, 1, etc)')"}" ;;
esac
shift
}
}
function get_opt {
local options
options="$(getopt --longoptions 'help,input-file:,offset:,output:,start:,end:,audio-input:,video-input:,time:,audio-stream:,camera:,subtitle-file:,subtitle-index:' \
--options 'hi:x:o:s:e:a:v:t:u:c:f:d:' \
--alternative -- "$@")"
eval set -- "$options"
while true; do
case "$1" in
-h|--help )
ffmpeg_help ;;
-i|--input-file )
shift
input_file="$1" ;;
-x|--offset )
shift
offset="$1" ;;
-o|--output )
shift
output="$1" ;;
-s|--start )
shift
start="$1" ;;
-e|--end )
shift
end="$1" ;;
-a|--audio-input )
shift
audio_input="$1" ;;
-v|--video-input )
shift
video_input="$1" ;;
-t|--time )
shift
time="$1" ;;
-u|--audio-stream )
shift
audio_stream="$1" ;;
-c|--camera )
shift
camera="$1" ;;
-f|--subtitle-file )
shift
sub_file="$1" ;;
-d|--subtitle-index )
shift
sub_index="$1" ;;
-- ) break ;;
esac
shift
done
}
get_opt "$@"
heading "$title"
main_items=( 'embed subtitle' 'burn subtitle (increases size)' 'burn embedded subtitle (increases size)' 'sync audio' 'sync video' 'trim' 'convert' 'remove audio' 'replace audio' 'screenshot' 'negate' 'reverse' 'add 0.3 saturation' 'show streams' 'audio stream to keep' 'available cameras' 'live camera' 'help' )
fzf__title=''
main_item="$(pipe_to_fzf "${main_items[@]}")" && wrap_fzf_choice "$main_item" || exit 37
case "$main_item" in
'embed subtitle' )
## https://stackoverflow.com/questions/8672809/use-ffmpeg-to-add-text-subtitles
prompt -i -f -o
ffmpeg -i "$input_file" -i "$sub_file" -c:v copy -c:a copy -c:s mov_text "$output" 2>/dev/null && accomplished ;;
'burn subtitle (increases size)' )
## https://stackoverflow.com/questions/8672809/use-ffmpeg-to-add-text-subtitles
prompt -i -f -o
ffmpeg -i "$input_file" -vf "subtitles=${sub_file}:force_style='FontSize=24,PrimaryColour=&H00f3f3&'" -c:a copy "$output" 2>/dev/null && accomplished ;;
## https://stackoverflow.com/questions/52338871/multicolor-text-in-ffmpeg
## "subtitles=${sub_file}:force_style='OutlineColour=&H80000000,BorderStyle=3,Outline=1,Shadow=0,MarginV=20,Borderstyle=4,BackColour=&H80000000,FontName=DejaVu Serif,FontSize=24,PrimaryColour=&H00f3f3&'" ## here it should be 00f3f3 instead of f3f300 (why??)
## Format: Name, Fontname Fontsize PrimaryColour SecondaryColour OutlineColour BackColour Bold Italic Underline StrikeOut ScaleX ScaleY Spacing Angle BorderStyle Outline Shadow Alignment MarginL MarginR MarginV Encoding
## Style: Default Arial 20 &H00FFFFFF &H000000FF &H00000000 &H00000000 0 0 0 0 100 100 0 0 1 2 2 2 10 10 10 1
'burn embedded subtitle (increases size)' )
## https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo
prompt -i -d -o
ffmpeg -i "$input_file" -vf "subtitles=${input_file}:stream_index=${sub_index}:force_style='FontSize=24,PrimaryColour=&H00f3f3&'" -c:a copy "$output" 2>/dev/null && accomplished ;;
## https://stackoverflow.com/questions/52338871/multicolor-text-in-ffmpeg
## "subtitles=${sub_file}:force_style='OutlineColour=&H80000000,BorderStyle=3,Outline=1,Shadow=0,MarginV=20,Borderstyle=4,BackColour=&H80000000,FontName=DejaVu Serif,FontSize=24,PrimaryColour=&H00f3f3&'" ## here it should be 00f3f3 instead of f3f300 (why??)
## Format: Name, Fontname Fontsize PrimaryColour SecondaryColour OutlineColour BackColour Bold Italic Underline StrikeOut ScaleX ScaleY Spacing Angle BorderStyle Outline Shadow Alignment MarginL MarginR MarginV Encoding
## Style: Default Arial 20 &H00FFFFFF &H000000FF &H00000000 &H00000000 0 0 0 0 100 100 0 0 1 2 2 2 10 10 10 1
'sync audio' )
prompt -i -x -o
ffmpeg -i "$input_file" -itsoffset "$offset" -i "$input_file" -map 0:v -map 1:a -c copy -y "$output" 2>/dev/null && accomplished ;;
'sync video' )
prompt -i -x -o
ffmpeg -i "$input_file" -itsoffset "$offset" -i "$input_file" -map 1:v -map 0:a -c copy -y "$output" 2>/dev/null && accomplished ;;
trim )
prompt -i -s -e -o
ffmpeg -ss "$start" -i "$input_file" -to "$end" -c copy -y "$output" 2>/dev/null && accomplished ;;
convert )
prompt -i -o
ffmpeg -i "$input_file" -q:a 1 -y "$output" 2>/dev/null && accomplished ;;
'remove audio' )
prompt -i -o
ffmpeg -i "$input_file" -an -vcodec copy -y "$output" 2>/dev/null && accomplished ;;
'replace audio' )
prompt -a -v -o
ffmpeg -i "$audio_input" -i "$video_input" -vcodec copy -acodec libmp3lame -y "$output" 2>/dev/null && accomplished ;;
screenshot )
prompt -i -t -o
ffmpeg -ss "$time" -i "$input_file" -frames:v 1 -y "$output" 2>/dev/null && accomplished ;;
negate )
prompt -i -o
ffmpeg -i "$input_file" -vf negate "$output" 2>/dev/null && accomplished ;;
reverse )
prompt -i -o
ffmpeg -i "$input_file" -vf reverse "$output" 2>/dev/null && accomplished ;;
'add 0.3 saturation' )
prompt -i -o
ffmpeg -i "$input_file" -vf eq=saturation=1.3 -c:a copy "$output" 2>/dev/null && accomplished ;;
'show streams' )
prompt -i
printf 'Streams for %s:\n' "$input_file"
ffmpeg -i "$input_file" 2>&1 | \grep -i 'Stream \#' | sed 's/^ //' 2>/dev/null && accomplished ;;
'audio stream to keep' )
prompt -i -u -o
ffmpeg -i "$input_file" -map 0:0 -map 0:"$audio_stream" -c copy "$output" 2>/dev/null && accomplished ;;
'available cameras' )
available_cameras="$(find /dev/ -mindepth 1 -maxdepth 1 -iname '*video*' | sort)"
printf '%s\n' "$available_cameras" ;;
'live camera' )
prompt -c
ffplay "$camera" ;;
help )
ffmpeg_help ;;
esac
## notes
## Add title:
# -metadata title='my title'
## Burn sub into a video
# ffmpeg -i <input> -vf subtitles=<subtitle> <output>
## Print connected webcams (requires ffmpeg) (https://trac.ffmpeg.org/wiki/Capture/Webcam):
# v4l2-ctl --list-devices
## Sync delayed audio (https://superuser.com/questions/982342/in-ffmpeg-how-to-delay-only-the-audio-of-a-mp4-video-without-converting-the-au)
# ffmpeg -i <input> -itsoffset -0.90 -i <input> -map 0:v -map 1:a -c copy <output>
## Sync delayed video (https://superuser.com/questions/982342/in-ffmpeg-how-to-delay-only-the-audio-of-a-mp4-video-without-converting-the-au)
# ffmpeg -i <input> -itsoffset -0.90 -i <input> -map 1:v -map 0:a -c copy <output>
## Trim video (https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg)
# ffmpeg -ss 00:00:03 -i <input> -to 00:04:00 -c copy <output>
## ╭────────────────────────┬───────────────────────────────────────────────╮
## │ Bitrate range (kbit/s) │ Option │
## ├────────────────────────┼───────────────────────────────────────────────┤
## │ 320 CBR (non VBR) │ -b:a 320k (NB this is 32KB/s, or its max) │ ◄ [highest quelity]
## │ 220-260 │ -q:a 0 (NB this is VBR from 22 to 26 KB/s) │
## │ 190-250 │ -q:a 1 │
## │ 170-210 │ -q:a 2 │
## │ 150-195 │ -q:a 3 │
## │ 140-185 │ -q:a 4 │
## │ 120-150 │ -q:a 5 │
## │ 100-130 │ -q:a 6 │
## │ 80-120 │ -q:a 7 │
## │ 70-105 │ -q:a 8 │
## │ 45-85 │ -q:a 9 │ ◄ [lowest quality]
## ├────────────────────────┴───────────────────────────────────────────────┤
## │ https://trac.ffmpeg.org/wiki/Encode/MP3 │
## ╰────────────────────────────────────────────────────────────────────────╯