1

My scripting knowledge in bash is very basic and I've cobbled together stuff in the past to accomplish some automation I need over the years.

The latest project is about some video processing, and based on a format of the file, I want to do some post-processing.

ffprobe will output meta-data about a video file, with the following excerpt:

ISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
DISPOSITION:timed_thumbnails=0
TAG:language=eng
[/STREAM]
[STREAM]
index=1
codec_name=dts
codec_long_name=DCA (DTS Coherent Acoustics)
profile=DTS
codec_type=audio
codec_time_base=1/48000
codec_tag_string=[0][0][0][0]

In particular, i want to detect whether or not the string codec_name=dts is present, then get the index value represented in the line prior. In the above case, it does contain codec_name=dts, and I want to be able to use the value 1 later in the script because of the line index=1 right before it.

In my script, I'm using this logic to do the first string detection using grep:

if (/usr/sbin/ffprobe -show_streams -i "$source"  | grep -q "codec_name=dts"); then
    <do some work here>
fi

What's the best way to do what I want to accomplish?

2 Answers 2

1

I suggest:

| awk -F "=" '$1=="index"{i=$2}; /codec_name=dts/{print i}'
Sign up to request clarification or add additional context in comments.

Comments

0

Using grep you can check whether index=? is followed by code_name=dts in the output. Something like the following:

#!/bin/bash

/usr/sbin/ffprobe -show_streams -i "$source" > metadatafile.txt
val=$(grep -B1 'codec_name=dts' metadatafile.txt  | grep -o -P '(?<=index=)[0-9]+$') 

# Make sure $val is a number, kinda redundant because above `grep` takes care of that.
if [[ $val =~ ^[0-9]+$ ]]; then
    echo "Doing some work here with $val"
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.