fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerMerge branch 'completions-to-use-expanded-tokens'a9618477e

master
13.3 KiB321 linesraw
1function __fish_ffmpeg_last_arg
2    echo (commandline -cx)[-1]
3end
4
5# Allow completions to match against an argument that includes a stream specifier, e.g. -c:v:2
6function __fish_ffmpeg_complete_regex
7    set -l regex $argv[1]
8    set -l completions $argv[2..-1]
9
10    complete -x -c ffmpeg -n "__fish_ffmpeg_last_arg | string match -rq -- '^'\"$regex\"'(\$|:)'" \
11        -a "$completions"
12end
13
14function __fish_ffmpeg_help_type
15    printf '%s\t%s\n' long "Print more options"
16    printf '%s\t%s\n' full "Print all options"
17
18    for help_type in decoder encoder demuxer muxer filter
19        set -l regex
20
21        if test $help_type = filter
22            set regex '\S+\s+(\S+)\s+\S+\s+(\S+)'
23        else
24            set regex '\S+\s+(\S+)\s+(\S+)'
25        end
26
27        printf '%s\n' $help_type=(ffmpeg -loglevel quiet -"$help_type"s | string trim \
28            | string match -rv '=|:$|^-' | string replace -rf "$regex" '$1\t$2')
29    end
30end
31
32function __fish_ffmpeg_codec_list
33    printf '%s\t%s\n' copy "Stream copy"
34
35    set -l identifier
36
37    switch $argv[1]
38        case video
39            set identifier '^V.*'
40        case audio
41            set identifier '^A.*'
42        case subtitle
43            set identifier '^S.*'
44        case all
45            set identifier '.*'
46        case '*'
47            return 1
48    end
49
50    printf '%s\n' (ffmpeg -loglevel quiet -decoders | string trim | string match -rv '=|:$|^-' \
51        | string match -r "$identifier" | string replace -rf '\S+\s+(\S+)\s+(\S+)' '$1\t$2')
52end
53
54function __fish_ffmpeg_pix_fmts
55    # We can't know in advance if the intention is to specify an output pix_fmt because the intent
56    # could be to instead provide a second input, but we can rule out an output if no input has
57    # been specified
58    set -l regex_filter '.'
59    if contains -- -i (commandline -cx)
60        set regex_filter '^I'
61    end
62    ffmpeg -hide_banner -loglevel quiet -pix_fmts |
63        # tail -n +9 | # skip past header
64        string match -rv '[:=-]|FLAGS' | # skip past header
65        string match -er $regex_filter |
66        string replace -rf '^[IOHPB.]{5} (\S+) .*' '$1'
67end
68
69function __fish_ffmpeg_filters
70    # TODO: Figure out how to distinguish {audio,video} source and destination, because some filters
71    # can go cross those lines (e.g. showvolume, which generates a video representation of the input
72    # audio).
73
74    set -l filter '.'
75    switch $argv[1]
76        case video
77            set filter '[V]\S*->|->\S*[V]'
78        case audio
79            set filter '[A]\S*->|->\S*[A]'
80        case all
81        case '*'
82            set filter '.'
83    end
84
85    ffmpeg -hide_banner -loglevel quiet -filters |
86        string match -e -- '->' | # skip past the header
87        string match -er $filter |
88        string replace -rf '^ [TSC.]{3} +(\S+) +\S+->\S+ +(.*)' '$1\t$2'
89end
90
91function __fish_ffmpeg_presets
92    set -l cmdline (commandline)
93    if string match -ireq '[hx]26[45]'
94        printf "%s\n" ultrafast superfast veryfast faster fast medium slow slower veryslow placebo
95    end
96end
97
98function __fish_ffmpeg_tunes
99    set -l cmdline (commandline)
100    if string match -req 264
101        printf "%s\n" film animation grain stillimage fastdecode zerolatency psnr ssim
102    end
103    if string match -req '265|hevc'
104        printf "%s\n" psnr ssim grain zerolatency fastdecode
105    end
106end
107
108function __fish_ffmpeg_crfs
109    seq 1 30
110end
111
112function __fish_ffmpeg_profile
113    if string match -req 264
114        printf "%s\n" baseline main high
115    end
116    if string match -req '265|hevc'
117        printf "%s\n" main high
118    end
119end
120
121function __fish_ffmpeg_formats
122    # TODO: Use heuristic to determine input vs output format and filter accordingly
123    ffmpeg -hide_banner -loglevel quiet -formats | string replace -rf '^ [DE.]{2} ([a-z0-9_]+) +(\S.+)$' '$1\t$2'
124end
125
126complete -c ffmpeg -s i -d "Specify input file"
127
128# Print help / information / capabilities
129complete -c ffmpeg -s L -d "Show license"
130complete -x -c ffmpeg -s h -s "?" -o help -l help -a "(__fish_ffmpeg_help_type)" -d "Show help"
131complete -c ffmpeg -o version -d "Show version"
132complete -c ffmpeg -o buildconf -d "Show build configuration"
133complete -c ffmpeg -o formats -d "Show available formats"
134complete -c ffmpeg -o muxers -d "Show available muxers"
135complete -c ffmpeg -o demuxers -d "Show available demuxers"
136complete -c ffmpeg -o devices -d "Show available devices"
137complete -c ffmpeg -o codecs -d "Show available codecs"
138complete -c ffmpeg -o decoders -d "Show available decoders"
139complete -c ffmpeg -o encoders -d "Show available encoders"
140complete -c ffmpeg -o bsfs -d "Show available bit stream filters"
141complete -c ffmpeg -o protocols -d "Show available protocols"
142complete -c ffmpeg -o filters -d "Show available filters"
143complete -c ffmpeg -o pix_fmts -d "Show available pixel formats"
144complete -c ffmpeg -o layouts -d "Show standard channel layouts"
145complete -c ffmpeg -o sample_fmts -d "Show available audio sample formats"
146complete -c ffmpeg -o colors -d "Show available color names"
147complete -c ffmpeg -o sources -d "List sources of the input device"
148complete -c ffmpeg -o sinks -d "List sinks of the output device"
149complete -c ffmpeg -o hwaccels -d "Show available HW acceleration methods"
150
151# Global options
152complete -x -c ffmpeg -o loglevel -s v \
153    -a "quiet panic fatal error warning info verbose debug trace" -d "Set logging level"
154complete -c ffmpeg -o report -d "Generate a report"
155complete -c ffmpeg -o max_alloc -d "Set maximum size of a single allocated block"
156complete -c ffmpeg -s y -d "Overwrite output files"
157complete -c ffmpeg -s n -d "Never overwrite output files"
158complete -c ffmpeg -o ignore_unknown -d "Ignore unknown stream types"
159complete -c ffmpeg -o filter_threads -d "Number of non-complex filter threads"
160complete -c ffmpeg -o filter_complex_threads -d "Number of threads for -filter_complex"
161complete -c ffmpeg -o stats -d "Print progress report during encoding"
162complete -c ffmpeg -o max_error_rate -d "Ratio of errors"
163complete -c ffmpeg -o bits_per_raw_sample -d "Set the number of bits per raw sample"
164complete -c ffmpeg -o vol -d "Change audio volume"
165
166# Per-file main options
167complete -c ffmpeg -s f -d "Force format" -xa "(__fish_ffmpeg_formats)"
168complete -c ffmpeg -s c -o codec -d "Codec name" -xa "(__fish_ffmpeg_codec_list all)"
169complete -c ffmpeg -o map_metadata -d "Set metadata information of outfile from infile"
170complete -c ffmpeg -s t -d "Record or transcode \"duration\" seconds of audio/video"
171complete -c ffmpeg -o to -d "Record or transcode stop time"
172complete -c ffmpeg -o fs -d "Set the limit file size in bytes"
173complete -c ffmpeg -o ss -d "Set the start time offset"
174complete -c ffmpeg -o seek_timestamp -d "Enable/disable seeking by timestamp with -ss"
175complete -c ffmpeg -o timestamp -d "Set the recording timestamp"
176complete -c ffmpeg -o metadata -d "Add metadata"
177complete -c ffmpeg -o program -d "Add program with specified streams"
178complete -x -c ffmpeg -o target -a '(for target in vcd svcd dvd dv dv50 ; echo "$target" ; \
179    echo pal-"$target" ; echo ntsc-"$target" ; echo film-"$target" ; end)' \
180    -d "Specify target file type"
181complete -c ffmpeg -o apad -d "Audio pad"
182complete -c ffmpeg -o frames -d "Set the number of frames to output"
183complete -c ffmpeg -o filter -d "Set stream filtergraph"
184complete -c ffmpeg -o filter_script -d "Read stream filtergraph description from a file"
185complete -c ffmpeg -o reinit_filter -d "Reinit filtergraph on input parameter changes"
186complete -c ffmpeg -o discard -d Discard
187complete -c ffmpeg -o disposition -d Disposition
188
189# Video options
190complete -c ffmpeg -o vframes -d "Set the number of video frames to output"
191complete -c ffmpeg -s r -d "Set frame rate"
192complete -c ffmpeg -s s -d "Set frame size"
193complete -c ffmpeg -o aspect -d "Set aspect ratio"
194complete -c ffmpeg -o bits_per_raw_sample -d "Set the number of bits per raw sample"
195complete -c ffmpeg -o vn -d "Disable video"
196complete -c ffmpeg -o vcodec -o "codec:v" -o "c:v"
197# Also list codecs when a particular stream is selected, e.g. -c:v:0
198__fish_ffmpeg_complete_regex "-(vcodec|c(odec)?:v)" "(__fish_ffmpeg_codec_list video)"
199complete -c ffmpeg -o timecode -d "Set initial TimeCode value"
200complete -x -c ffmpeg -o pass -a "1 2 3" -d "Select the pass number"
201complete -c ffmpeg -o vf -d "Set video filters"
202complete -c ffmpeg -o ab -o "b:a" -d "Audio bitrate"
203complete -c ffmpeg -s b -o "b:v" -d "Video bitrate"
204complete -c ffmpeg -o dn -d "Disable data"
205# Advanced video options
206complete -c ffmpeg -o pix_fmt
207__fish_ffmpeg_complete_regex -pix_fmt "(__fish_ffmpeg_pix_fmts)"
208
209# Audio options
210complete -c ffmpeg -o aframes -d "Set the number of audio frames to output"
211complete -c ffmpeg -o aq -d "Set audio quality"
212complete -c ffmpeg -o ar -d "Set audio sampling rate"
213complete -c ffmpeg -o ac -d "Set number of audio channels"
214complete -c ffmpeg -o an -d "Disable audio"
215complete -c ffmpeg -o acodec -o "codec:a" -o "c:a"
216__fish_ffmpeg_complete_regex '-(acodec|c(odec)?:a)' "(__fish_ffmpeg_codec_list audio)"
217complete -c ffmpeg -o vol -d "Change audio volume"
218complete -c ffmpeg -o af -d "Set audio filters"
219
220# Subtitle options
221complete -c ffmpeg -s s -d "Set frame size"
222complete -c ffmpeg -o sn -d "Disable subtitle"
223complete -c ffmpeg -o scodec -o "codec:s" -o "c:s"
224__fish_ffmpeg_complete_regex '-(scodec|c(odec)?:s)(:\d+)?' "(__fish_ffmpeg_codec_list subtitle)"
225complete -c ffmpeg -o stag -d "Force subtitle tag/fourcc"
226complete -c ffmpeg -o fix_sub_duration -d "Fix subtitles duration"
227complete -c ffmpeg -o canvas_size -d "Set canvas size"
228complete -c ffmpeg -o spre -d "Set the subtitle options to the indicated preset"
229
230# Codec-specific options
231complete -c ffmpeg -o pre -o preset -d "Preset name"
232__fish_ffmpeg_complete_regex 'pre(set)?' "(__fish_ffmpeg_presets)"
233complete -c ffmpeg -o tune
234__fish_ffmpeg_complete_regex tune "(__fish_ffmpeg_tunes)"
235complete -c ffmpeg -o crf -o q
236__fish_ffmpeg_complete_regex 'crf|q' "(__fish_ffmpeg_crfs)"
237complete -c ffmpeg -o profile
238__fish_ffmpeg_complete_regex profile "(__fish_ffmpeg_profiles)"
239
240# Filters
241#
242# fish completions are not designed to take the current argument being completed as an input,
243# and if introspected it is normally used to filter/constrain the completions generated. ffmpeg's
244# command line syntax is extremely nasty and in addition to using stringification for all arguments,
245# relies on special characters to delimit separate subarguments (rather than, e.g., concatenating
246# repeated invocations of the same switch), meaning the typical way of generating fish completions
247# would only allow us to generate the very first subargument in a complex argument payload.
248#
249# We hack around this in a ridiculously ugly fashion by always generating completions that are
250# prepended with a prefix of the current value of the payload in question.
251#
252# Top-level ffmpeg filters are comma separated, but take optional arguments that might themselves
253# take optional subarguments. While = is a key=value delimiter, it is also a prefix for the first
254# argument to a filter, e.g. filter_name=opt1 and filter_name=opt1=opt_value, and in the case that
255# a filter takes more than one argument, filter arguments are delimited with :
256#
257# A complete example:
258#
259# ffmpeg .... -filter:v filter1,filter2=f2_arg1=f2_arg1_val:f2_arg2
260
261function __fish_ffmpeg_split_filter_graph
262    set -l filters (string split , $argv[1])
263    printf "%s\n" $filters
264end
265
266# Given a single filter expression, emits the filter name on the first line and then each key=value
267# pair of arguments to the filter on each subsequent line.
268function __fish_ffmpeg_decompose_filter
269    set -l parts (string split -m 1 = "$argv")
270    echo $parts[1] # the filter name
271    set -l arguments (string split : $parts[2])
272    printf "%s\n" $arguments
273end
274
275function __fish_ffmpeg_concat_filters
276    string join -- , $argv
277end
278
279function __fish_ffmpeg_concat_filter_args
280    string join -- : $argv
281end
282
283function __fish_ffmpeg_complete_filter
284    set -l filter_type all
285    if string match -rq -- '^-(vf(ilter)?|f(ilter)?:v)' (__fish_ffmpeg_last_arg)
286        set filter_type video
287    else if string match -rq -- '^-(af(ilter)?|f(ilter)?:a)' (__fish_ffmpeg_last_arg)
288        set filter_type audio
289    end
290
291    # echo -e "\n **** $filter_type **** \n" > /dev/tty
292
293    set -l filters_arg (commandline -xp)[-1]
294    if string match -rq -- '^-' $filters_arg
295        # No filter name started
296        __fish_ffmpeg_filters $filter_type
297        return
298    end
299
300    set -l filters (__fish_ffmpeg_split_filter_graph $filters_arg)
301    # We are completing only the last filter (in case there are multiple)
302    set -l filter $filters[-1]
303    # Remove it from the list of passed-through filters
304    set -e filters[-1]
305    # `ffmpeg -h filter=FILTER` exposes information that can be used to dynamically complete not
306    # only the name of the filter but also the name of its individual options. We currently only
307    # support completing the name of the filter.
308    set -l decomposed (__fish_ffmpeg_decompose_filter $filter)
309    set -l filter_name $decomposed[1]
310    set -l filter_args (__fish_ffmpeg_concat_filter_args $decomposed[2..-1])
311
312    # Emit the mutated filter graph, with permutations of the final filter name offered as
313    # completions.
314    for known_filter in (__fish_ffmpeg_filters $filter_type)
315        set -l modified_filter (string join = $known_filter $filter_args)
316        __fish_ffmpeg_concat_filters $filters $modified_filter
317    end
318end
319
320complete -x -c ffmpeg -o filter -o filter:v -o filter:s -o filter:a -o vf -o af \
321    -a "(__fish_ffmpeg_complete_filter)"