Skip to content

Instantly share code, notes, and snippets.

@pdkl95
Created October 10, 2012 21:30
Show Gist options
  • Save pdkl95/3868552 to your computer and use it in GitHub Desktop.
Save pdkl95/3868552 to your computer and use it in GitHub Desktop.
mident - a wrapper for a more reliable "mplayer -identify"
#!/bin/bash
#####################################################
###/ \### #
### mident ### This is a wrapper script around #
###\ /### the "-identify" feature provided #
################ by mplayer/mplayer2, similar to #
# the "mplayer.sh" script that is #
# distributed with mplayer itself. This version #
# should be significantly more reliable, easier #
# to customize. The output is parsable by bash: #
# #
# eval $(mident somefile.mkv) #
# #
# One change in behavior is that this script will #
# only process a single file at a time. This is #
# by design, as it would be ambiguous which file #
# a variale "${ID_VIDEO_CODEC}" is referencing. #
# #
# Any ANSI escape sequences foudn in the data we #
# receive from mplayer are stripped. This is done #
# by first searching the $PATH to see if the tool #
# "ansifilter" is available. Otherwise, a regexp #
# based fallback will be used instead, that isn't #
# as well-tested hand has the serious limitation #
# of *ONLY* filtering color codes. Fortunately, #
# color codes are the only ANSI escape sequence #
# in common use, so the fallback is usually fine. #
# #
# ChangeLog: #
# 2012-10-10 <[email protected]> #
# - initial version created & released #
# #
#####################################################
########################################################################
# Copyright 2012 Brent Sanders #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version, which is available in various #
# formats at <http://www.gnu.org/licenses/gpl.html>. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
########################################################################
if (( $# != 1 )) ; then
echo "Usage: $(basename "$0") <file>"
exit 1
fi
declare mp_bin="${MPLAYER:-mplayer2}"
declare -a mp_opt=( -nomsgcolor -vo null -ao null -frames 0 )
case ${mp_mode:-verbose} in
verbose) mp_opt+=( -msglevel identify=6 ) ;;
*|normal) mp_opt+=( -msglevel identify=4 ) ;;
esac
if ! command hash ansifilter 2>&- ; then
# The external tool "ansifilter" is much more reliable,
# but a regex fallback works for the common case of just
# having ot filter color codes.
#
# NOTE: this fallback *only* strips color codes, other
# escape codes will probably make it through!
ansifilter() {
sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g"
}
fi
mp_identify() {
echo "ID_MIDENTIFY_BIN=${mp_bin}"
echo "ID_MIDENTIFY_OPT=${mp_opt[@]}"
"${mp_bin}" "${mp_opt[@]}" -v "$@" 2>/dev/null
}
id_lines_only() {
egrep '^ID_'
}
# this should make it so we can re-parse
# our output in bash with a simple:
# eval $(midentify file.mkv)
quote_values() {
local line
while read -r -s line ; do
printf "${line%%=*}=%q\n" "${line#*=}"
done
}
# the output is sorted to make diffs of the media ID more reliable
filter_mp_output() {
ansifilter | id_lines_only | quote_values | sort --stable
}
identify() {
mp_identify "$@" 2>/dev/null | filter_mp_output
}
identify "${1}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment