2012-08-29 13:58:48 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# file identification script
|
|
|
|
#
|
|
|
|
# manual usage:
|
2012-10-11 00:04:08 +00:00
|
|
|
# mpv_identify.sh foo.mkv
|
2012-08-29 13:58:48 +00:00
|
|
|
#
|
|
|
|
# sh/dash/ksh/bash usage:
|
2012-10-11 00:04:08 +00:00
|
|
|
# . mpv_identify.sh FOO_ foo.mkv
|
2012-08-29 13:58:48 +00:00
|
|
|
# will fill properties into variables like FOO_length
|
|
|
|
#
|
|
|
|
# zsh usage:
|
2012-10-11 00:04:08 +00:00
|
|
|
# mpv_identify() { emulate -L sh; . mpv_identify.sh "$@"; }
|
|
|
|
# mpv_identify FOO_ foo.mkv
|
2012-08-29 13:58:48 +00:00
|
|
|
# will fill properties into variables like FOO_length
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2014-08-30 16:30:56 +00:00
|
|
|
__midentify__main() {
|
|
|
|
|
|
|
|
case "$0" in
|
|
|
|
mpv_identify.sh|*/mpv_identify.sh)
|
|
|
|
# we are NOT being sourced
|
2014-08-30 16:31:38 +00:00
|
|
|
[ -n "$1" ] && set -- '' "$@"
|
2014-08-30 16:30:56 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2014-08-30 16:31:38 +00:00
|
|
|
if [ "$#" -lt 2 ]; then
|
2014-09-16 17:01:35 +00:00
|
|
|
cat >&2 <<EOF
|
|
|
|
Usage 1 (for humans only): $0 filename.mkv
|
|
|
|
will print all property values.
|
|
|
|
Note that this output really shouldn't be parsed, as the
|
|
|
|
format is subject to change.
|
|
|
|
|
|
|
|
Usage 2 (for use by scripts): see top of this file
|
|
|
|
|
|
|
|
NOTE: for mkv with ordered chapters, this may
|
|
|
|
not always identify the specified file, but the
|
|
|
|
file providing the first chapter. Specify
|
|
|
|
--no-ordered-chapters to prevent this.
|
|
|
|
EOF
|
|
|
|
return 2
|
2014-08-30 16:30:56 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
local LF="
|
|
|
|
"
|
|
|
|
|
2014-08-30 16:31:38 +00:00
|
|
|
local nextprefix="$1"
|
2014-08-30 16:30:56 +00:00
|
|
|
shift
|
|
|
|
|
|
|
|
if [ -n "$nextprefix" ]; then
|
|
|
|
# in case of error, we always want this unset
|
2014-08-30 16:31:38 +00:00
|
|
|
unset "${nextprefix}path"
|
2014-08-30 16:30:56 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
local allprops="
|
|
|
|
filename
|
|
|
|
path
|
|
|
|
stream-start
|
|
|
|
stream-end
|
|
|
|
stream-length
|
|
|
|
|
|
|
|
demuxer
|
|
|
|
|
|
|
|
length
|
|
|
|
chapters
|
|
|
|
editions
|
|
|
|
titles
|
|
|
|
|
|
|
|
audio
|
|
|
|
audio-bitrate
|
|
|
|
audio-codec
|
2016-11-22 14:38:01 +00:00
|
|
|
audio-codec-name
|
2014-08-30 16:30:56 +00:00
|
|
|
|
|
|
|
video
|
|
|
|
angle
|
|
|
|
video-bitrate
|
|
|
|
video-codec
|
|
|
|
video-format
|
|
|
|
video-aspect
|
|
|
|
fps
|
|
|
|
width
|
|
|
|
height
|
|
|
|
dwidth
|
|
|
|
dheight
|
|
|
|
|
|
|
|
sub
|
|
|
|
"
|
|
|
|
# TODO add metadata support once mpv can do it
|
|
|
|
|
|
|
|
local propstr="X-MIDENTIFY-START:$LF"
|
|
|
|
local key
|
|
|
|
for key in $allprops; do
|
2014-08-30 16:31:38 +00:00
|
|
|
propstr="${propstr}X-MIDENTIFY: $key \${=$key}$LF"
|
|
|
|
key="$(printf '%s\n' "$key" | tr - _)"
|
|
|
|
unset "$nextprefix$key"
|
2014-08-30 16:30:56 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
local fileindex=0
|
|
|
|
local prefix=
|
2014-09-16 16:23:04 +00:00
|
|
|
local line
|
|
|
|
while IFS= read -r line; do
|
2014-08-30 16:30:56 +00:00
|
|
|
case "$line" in
|
|
|
|
X-MIDENTIFY-START:)
|
|
|
|
if [ -n "$nextprefix" ]; then
|
2014-08-30 16:31:38 +00:00
|
|
|
prefix="$nextprefix"
|
|
|
|
if [ "$fileindex" -gt 0 ]; then
|
|
|
|
nextprefix="${prefix%${fileindex}_}"
|
2014-08-30 16:30:56 +00:00
|
|
|
fi
|
2014-09-16 00:49:47 +00:00
|
|
|
fileindex="$((fileindex+1))"
|
2014-08-30 16:31:38 +00:00
|
|
|
nextprefix="${nextprefix}${fileindex}_"
|
2014-08-30 16:30:56 +00:00
|
|
|
for key in $allprops; do
|
2014-08-30 16:31:38 +00:00
|
|
|
key="$(printf '%s\n' "$key" | tr - _)"
|
|
|
|
unset "$nextprefix$key"
|
2014-08-30 16:30:56 +00:00
|
|
|
done
|
|
|
|
else
|
2014-08-30 16:31:38 +00:00
|
|
|
if [ "$fileindex" -gt 0 ]; then
|
2014-09-16 17:01:35 +00:00
|
|
|
printf '\n'
|
2014-08-30 16:30:56 +00:00
|
|
|
fi
|
2014-09-16 00:49:47 +00:00
|
|
|
fileindex="$((fileindex+1))"
|
2012-08-29 13:58:48 +00:00
|
|
|
fi
|
2014-08-30 16:30:56 +00:00
|
|
|
;;
|
|
|
|
X-MIDENTIFY:\ *)
|
2014-09-21 18:26:11 +00:00
|
|
|
local key="${line#X-MIDENTIFY: }"
|
2014-08-30 16:31:38 +00:00
|
|
|
local value="${key#* }"
|
|
|
|
key="${key%% *}"
|
|
|
|
key="$(printf '%s\n' "$key" | tr - _)"
|
2014-08-30 16:30:56 +00:00
|
|
|
if [ -n "$nextprefix" ]; then
|
|
|
|
if [ -z "$prefix" ]; then
|
|
|
|
echo >&2 "Got X-MIDENTIFY: without X-MIDENTIFY-START:"
|
|
|
|
elif [ -n "$value" ]; then
|
2014-08-30 16:31:38 +00:00
|
|
|
eval "$prefix$key"='"$value"'
|
2014-08-30 16:30:56 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ -n "$value" ]; then
|
2014-09-16 17:01:35 +00:00
|
|
|
printf '%s=%s\n' "$key" "$value"
|
2014-08-30 16:30:56 +00:00
|
|
|
fi
|
2012-08-29 13:58:48 +00:00
|
|
|
fi
|
2014-08-30 16:30:56 +00:00
|
|
|
;;
|
|
|
|
esac
|
2014-09-16 16:23:04 +00:00
|
|
|
done <<EOF
|
|
|
|
$(${MPV:-mpv} --term-playing-msg="$propstr" --vo=null --ao=null \
|
|
|
|
--frames=1 --quiet --no-cache --no-config -- "$@")
|
|
|
|
EOF
|
2014-08-30 16:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__midentify__main "$@"
|