1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-30 02:52:10 +00:00

TOOLS/mpv_identify.sh: move script body to a function

Don't prepend each variable with __midentify__, just make them local
to the function.
This commit is contained in:
shdown 2014-08-30 20:30:56 +04:00 committed by wm4
parent cbe03b6255
commit 880d5c8a4a

102
TOOLS/mpv_identify.sh Executable file → Normal file
View File

@ -17,6 +17,8 @@
# When multiple files were specified, their info will be put into FOO_* for the # When multiple files were specified, their info will be put into FOO_* for the
# first file, FOO_1_* for the second file, FOO_2_* for the third file, etc. # first file, FOO_1_* for the second file, FOO_2_* for the third file, etc.
__midentify__main() {
case "$0" in case "$0" in
mpv_identify.sh|*/mpv_identify.sh) mpv_identify.sh|*/mpv_identify.sh)
# we are NOT being sourced # we are NOT being sourced
@ -42,25 +44,25 @@ if [ $# -lt 2 ]; then
echo >&2 "not always identify the specified file, but the" echo >&2 "not always identify the specified file, but the"
echo >&2 "file providing the first chapter. Specify" echo >&2 "file providing the first chapter. Specify"
echo >&2 "--no-ordered-chapters to prevent this." echo >&2 "--no-ordered-chapters to prevent this."
exit 1 return 1
fi fi
if [ -z "$MPV" ]; then if [ -z "$MPV" ]; then
MPV="mpv" local MPV="mpv"
fi fi
__midentify__LF=" local LF="
" "
__midentify__nextprefix=$1 local nextprefix=$1
shift shift
if [ -n "$__midentify__nextprefix" ]; then if [ -n "$nextprefix" ]; then
# in case of error, we always want this unset # in case of error, we always want this unset
eval unset $__midentify__nextprefix'path' eval unset $nextprefix'path'
fi fi
__midentify__allprops=" local allprops="
filename filename
path path
stream-start stream-start
@ -97,77 +99,71 @@ __midentify__allprops="
" "
# TODO add metadata support once mpv can do it # TODO add metadata support once mpv can do it
__midentify__propstr="X-MIDENTIFY-START:$__midentify__LF" local propstr="X-MIDENTIFY-START:$LF"
for __midentify__key in $__midentify__allprops; do local key
__midentify__propstr=$__midentify__propstr"X-MIDENTIFY: $__midentify__key \${=$__midentify__key}$__midentify__LF" for key in $allprops; do
__midentify__key=`echo "$__midentify__key" | tr - _` propstr=$propstr"X-MIDENTIFY: $key \${=$key}$LF"
eval unset $__midentify__nextprefix$__midentify__key key=`echo "$key" | tr - _`
eval unset $nextprefix$key
done done
__midentify__output=`$MPV --term-playing-msg="$__midentify__propstr" --vo=null --ao=null --frames=1 --quiet --no-cache --no-config "$@"` local output=`$MPV --term-playing-msg="$propstr" --vo=null --ao=null --frames=1 --quiet --no-cache --no-config "$@"`
__midentify__fileindex=0 local fileindex=0
__midentify__prefix= local prefix=
while :; do while :; do
case "$__midentify__output" in local line output
case "$output" in
'') '')
break break
;; ;;
*$__midentify__LF*) *$LF*)
__midentify__line=${__midentify__output%%$__midentify__LF*} line=${output%%$LF*}
__midentify__output=${__midentify__output#*$__midentify__LF} output=${output#*$LF}
;; ;;
*) *)
__midentify__line=$__midentify__output line=$output
__midentify__output= output=
;; ;;
esac esac
case "$__midentify__line" in case "$line" in
X-MIDENTIFY-START:) X-MIDENTIFY-START:)
if [ -n "$__midentify__nextprefix" ]; then if [ -n "$nextprefix" ]; then
__midentify__prefix=$__midentify__nextprefix prefix=$nextprefix
if [ $__midentify__fileindex -gt 0 ]; then if [ $fileindex -gt 0 ]; then
__midentify__nextprefix=${__midentify__prefix%$__midentify__fileindex\_} nextprefix=${prefix%$fileindex\_}
fi fi
__midentify__fileindex=$(($__midentify__fileindex+1)) fileindex=$(($fileindex+1))
__midentify__nextprefix=$__midentify__nextprefix$__midentify__fileindex\_ nextprefix=$nextprefix$fileindex\_
for __midentify__key in $__midentify__allprops; do for key in $allprops; do
__midentify__key=`echo "$__midentify__key" | tr - _` key=`echo "$key" | tr - _`
eval unset $__midentify__nextprefix$__midentify__key eval unset $nextprefix$key
done done
else else
if [ $__midentify__fileindex -gt 0 ]; then if [ $fileindex -gt 0 ]; then
echo echo
fi fi
__midentify__fileindex=$(($__midentify__fileindex+1)) fileindex=$(($fileindex+1))
fi fi
;; ;;
X-MIDENTIFY:\ *) X-MIDENTIFY:\ *)
__midentify__key=${__midentify__line#X-MIDENTIFY:\ } key=${line#X-MIDENTIFY:\ }
__midentify__value=${__midentify__key#* } local value=${key#* }
__midentify__key=${__midentify__key%% *} key=${key%% *}
__midentify__key=`echo "$__midentify__key" | tr - _` key=`echo "$key" | tr - _`
if [ -n "$__midentify__nextprefix" ]; then if [ -n "$nextprefix" ]; then
if [ -z "$__midentify__prefix" ]; then if [ -z "$prefix" ]; then
echo >&2 "Got X-MIDENTIFY: without X-MIDENTIFY-START:" echo >&2 "Got X-MIDENTIFY: without X-MIDENTIFY-START:"
elif [ -n "$__midentify__value" ]; then elif [ -n "$value" ]; then
eval $__midentify__prefix$__midentify__key=\$__midentify__value eval $prefix$key=\$value
fi fi
else else
if [ -n "$__midentify__value" ]; then if [ -n "$value" ]; then
echo "$__midentify__key=$__midentify__value" echo "$key=$value"
fi fi
fi fi
;; ;;
esac esac
done done
}
unset __midentify__fileindex __midentify__main "$@"
unset __midentify__allprops
unset __midentify__key
unset __midentify__LF
unset __midentify__line
unset __midentify__output
unset __midentify__nextprefix
unset __midentify__prefix
unset __midentify__propstr
unset __midentify__value