Parsing FFprobe XML in BASH
Posted: Fri Jan 08, 2016 4:49 am
HowTo parse an FFprobe XML file in a BASH script, using XMLStarlet:
Code: Select all
function parse_ffprobe_xml
{
local XML_FILE="$1"
local CODEC_TYPE="$2"
local XPATH_QUERY="/ffprobe/streams/stream[@codec_type='$CODEC_TYPE']"
PROBED_PIXFMT=$($XMLSTARLET sel -t -v "$XPATH_QUERY/@pix_fmt" "$XML_FILE")
PROBED_WIDTH=$($XMLSTARLET sel -t -v "$XPATH_QUERY/@width" "$XML_FILE")
PROBED_FPS=$($XMLSTARLET sel -t -v "$XPATH_QUERY/@r_frame_rate" "$XML_FILE")
PROBED_DURATION=$($XMLSTARLET sel -t -v "$XPATH_QUERY/@duration" "$XML_FILE")
PROBED_DURATION_TS=$($XMLSTARLET sel -t -v "$XPATH_QUERY/@duration_ts" "$XML_FILE")
# Round FPS to 3 digits after the comma:
ROUNDED_FPS=$(echo "scale=3;$PROBED_FPS" | bc -l)
# Round duration to nanoseconds (=9 digits):
ROUNDED_DURATION=$(echo "scale=9;$PROBED_DURATION/1" | bc -l)
}