zsh-workers
 help / color / mirror / code / Atom feed
From: Haakon Riiser <haakon.riiser@fys.uio.no>
To: <zsh-workers@zsh.org>
Subject: Re: Small patch for FFmpeg completion function
Date: Thu, 28 Apr 2011 16:46:19 +0200	[thread overview]
Message-ID: <d6702e01c19e055937abd76692831f33@ulrik.uio.no> (raw)
In-Reply-To: <a2cecc617a7c8fe0f5e16c90aa941989@ulrik.uio.no>

[-- Attachment #1: Type: text/plain, Size: 775 bytes --]

 On Tue, 26 Apr 2011 23:48:21 +0200, Haakon Riiser 
 <haakon.riiser@fys.uio.no> wrote:
> This is a patch for Completion/Unix/Command/_ffmpeg (CVS rev 1.2)
> It adds the "copy" keyword as an extra completion to -acodec and 
> -vcodec.

 Decided to clean it up further. Obviously it is better to avoid having 
 Perl as a dependency, so the attached version does everything using zsh 
 scripting. It also uses _call_program and $words[1] instead of "ffmpeg" 
 directly.

 No differences other than that: The completion function should produce 
 the same results as the previous one that used Perl. Because of the 
 large diff, the entire completion function is attached. It is based on 
 the last version I sent (which again is based on the last version in 
 CVS).

-- 
  Haakon

[-- Attachment #2: _ffmpeg --]
[-- Type: application/octet-stream, Size: 6489 bytes --]

#compdef ffmpeg

local context state line
typeset -A opt_args

local -a _ffmpeg_argspecs

local BOLD=$'\e[1m'
local NORM=$'\e[m'

(( $+functions[_ffmpeg_compadd] )) || _ffmpeg_compadd() {
    compadd -X "${BOLD}$1${NORM}" -q -S "$3" -a $2
}

(( $+functions[_ffmpeg_presets] )) || _ffmpeg_presets() {
    local presets
    presets=(~/.ffmpeg/*.ffpreset(:t:r) "$FFMPEG_DATADIR"/*.ffpreset(:t:r))
    _ffmpeg_compadd 'select preset' presets ''
}

(( $+functions[_ffmpeg_acodecs] )) || _ffmpeg_acodecs() {
    local acodecs
    acodecs=(copy ${${(M)${(f)"$(_call_program audio-codecs $words[1] -codecs 2>/dev/null)"}:#[[:space:]][D[:space:]][E[:space:]]A[S[:space:]][D[:space:]][T[:space:]][[:space:]][^[:space:]]##*}//(#b)????????([^[:space:]]##)*/$match[1]})
    _ffmpeg_compadd 'force audio codec (''copy'' to copy stream)' acodecs ''
}

(( $+functions[_ffmpeg_vcodecs] )) || _ffmpeg_vcodecs() {
    local vcodecs
    vcodecs=(copy ${${(M)${(f)"$(_call_program video-codecs $words[1] -codecs 2>/dev/null)"}:#[[:space:]][D[:space:]][E[:space:]]V[S[:space:]][D[:space:]][T[:space:]][[:space:]][^[:space:]]##*}//(#b)????????([^[:space:]]##)*/$match[1]})
    _ffmpeg_compadd 'force video codec (''copy'' to copy stream)' vcodecs ''
}

(( $+functions[_ffmpeg_formats] )) || _ffmpeg_formats() {
    local formats
    formats=(${(ou)${=${(s:,:)${${(M)${(f)"$(_call_program formats $words[1] -formats 2>/dev/null)"}:#[[:space:]][D[:space:]][E[:space:]][[:space:]][^[:space:]]##*}//(#b)????([^[:space:]]##)*/$match[1]}}}})
    _ffmpeg_compadd 'force format' formats ''
}

(( $+functions[_ffmpeg_list_pix_fmts] )) || _ffmpeg_list_pix_fmts() {
    print -l ${${(M)${(f)"$(_call_program pix-fmts $words[1] -pix_fmts 2>/dev/null)"}:#[I.]*}//(#b)??????([^[:space:]]##)*/$match[1]}
}

(( $+functions[_ffmpeg_pix_fmts] )) || _ffmpeg_pix_fmts() {
    local pix_fmts
    pix_fmts=($(_ffmpeg_list_pix_fmts))
    _ffmpeg_compadd 'set pixel format' pix_fmts ''
}

(( $+functions[_ffmpeg_bsfs] )) || _ffmpeg_bsfs() {
    local bsfs
    bsfs=(${${(f)"$(_call_program bsfs $words[1] -bsfs 2>/dev/null)"}:#*:})
    _ffmpeg_compadd 'set bitstream filter' bsfs ''
}

{
    local lastopt
    local lastopt_description
    local lastopt_takesargs
    local -a lastopt_values

    _call_program options $words[1] -h 2>/dev/null | while IFS=$'\n' read -r; do
        if [[ $REPLY == -* ]]; then
            if [[ -n $lastopt ]]; then
                if (( lastopt_takesargs )); then
                    lastopt+=":$lastopt_description:"
                    if (( $#lastopt_values )); then
                        lastopt+="(${lastopt_values[*]})"
                    fi
                fi
                _ffmpeg_argspecs+=$lastopt
            fi
            lastopt=${REPLY%%[[:space:]]*}
            lastopt_description=${REPLY##-[^[:space:]]##[[:space:]]##}
            if [[ $lastopt_description == '<'* ]]; then
                lastopt_description=${lastopt_description##<[^[:space:]]##>[[:space:]]##[^[:space:]]##[[:space:]]#}
                if [[ -z $lastopt_description ]]; then
                    lastopt_description=$lastopt
                fi
                lastopt_description=${lastopt_description//:/\\:}
            elif [[ $lastopt_description == [^[:space:]]##[[:space:]][[:space:]]* ]]; then
                local example=${lastopt_description%% *}
                example=${example//:/\\:}
                lastopt_description=${lastopt_description##[^[:space:]]##[[:space:]]##}
                lastopt_description=${lastopt_description//:/\\:}
                if [[ $example == filename ]]; then
                    lastopt_takesargs=0
                    lastopt+=":$lastopt_description:_files"
                elif [[ $lastopt == -[asv]pre ]]; then
                    lastopt_takesargs=0
                    lastopt+=": :_ffmpeg_presets"
                elif [[ $lastopt == -acodec ]]; then
                    lastopt_takesargs=0
                    lastopt+=": :_ffmpeg_acodecs"
                elif [[ $lastopt == -vcodec ]]; then
                    lastopt_takesargs=0
                    lastopt+=": :_ffmpeg_vcodecs"
                elif [[ $lastopt == -f ]]; then
                    lastopt_takesargs=0
                    lastopt+=": :_ffmpeg_formats"
                elif [[ $lastopt == -pix_fmt ]]; then
                    lastopt_takesargs=0
                    lastopt+=": :_ffmpeg_pix_fmts"
                elif [[ $example == bitstream_filter ]]; then
                    lastopt_takesargs=0
                    lastopt+=": :_ffmpeg_bsfs"
                else
                    lastopt_takesargs=1
                    lastopt_description+=" ($example)"
                fi
            else
                lastopt_takesargs=0
                if [[ $lastopt == -vfilters ]]; then
                    lastopt+=": :->vfilters"
                fi
            fi
            lastopt_values=()
        elif [[ $REPLY == ' '* ]]; then
            REPLY=${REPLY##[[:space:]]##}
            REPLY=${REPLY%%[[:space:]]##*}
            lastopt_takesargs=1
            lastopt_values+=$REPLY
        fi
    done
    if [[ -n $lastopt ]]; then
        if (( lastopt_takesargs )); then
            lastopt+=":$lastopt_description:"
            if (( $#lastopt_values )); then
                lastopt+="(${lastopt_values[*]})"
            fi
        fi
        _ffmpeg_argspecs+=$lastopt
    fi
}

_arguments -S \
    "${_ffmpeg_argspecs[@]}" \
    '*:output file:_files' \
    && return 0

[[ "$state" == "vfilters" ]] &&
    _values -s , -S = 'video filters' \
    'aspect:set aspect ratio (rational number X\:Y or decimal number):' \
    'crop:crop input video (x\:y\:width\:height):' \
    'format: :->format' \
    'noformat: :->noformat' \
    'null' \
    'pad:add pads to the input image (width\:height\:x\:y\:color_string):' \
    'pixelaspect:set pixel aspect ratio (rational number X\:Y or decimal number):' \
    'scale:scale input video (width\:height):' \
    'slicify:output slice height ("random" or a number of pixels):' \
    'unsharp:luma_x\:luma_y\:luma_amount\:chroma_x\:chroma_y\:chroma_amount:' \
    'vflip' \
    'buffer' \
    'nullsrc' \
    'nullsink' \
    && return 0

[[ "$state" == "format" ]] &&
    _values -s : -S = 'convert input video to one of the specified pixel formats' $(_ffmpeg_list_pix_fmts) && return 0

[[ "$state" == "noformat" ]] &&
    _values -s : -S = 'disable specified pixel formats by force' $(_ffmpeg_list_pix_fmts) && return 0

return 1

  reply	other threads:[~2011-04-28 15:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-26 21:48 Haakon Riiser
2011-04-28 14:46 ` Haakon Riiser [this message]
2011-04-30 20:59   ` Peter Stephenson
2011-04-30 23:03     ` Haakon Riiser

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d6702e01c19e055937abd76692831f33@ulrik.uio.no \
    --to=haakon.riiser@fys.uio.no \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).