LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Need to detect if a video has black borders (https://www.linuxquestions.org/questions/linux-software-2/need-to-detect-if-a-video-has-black-borders-4175694601/)

GPGAgent 05-02-2021 06:55 AM

Need to detect if a video has black borders
 
I can detect the black borders on a video and create a crop= parameter for ffmpeg like this:
Code:

# detect the cropping parameters
FFREPORT=file=CROPDATA.txt:level=32 ffmpeg -hide_banner -loglevel quiet -ss $1 -i $2  -vframes 2 -vf "cropdetect=24:16:0" -f null -

# Extract the cropping paramaters for the -vf switch
CROP=`awk  '/crop=/ {print $14}' CROPDATA.txt`


This works just fine
Code:

$ echo $CROP
crop=976:720:156:0

What I want to do is to check the last two crop parameters x and y and if both are zero the vdio has no black borders.


How can I extract these two values, 156 and 0.


What i would do is add these together and if their sum is zero then there is no border. If it's greater thatn one there is a border and my script can take the appropriate action


Seems simple, but I can't figure it out!

GPGAgent 05-02-2021 08:08 AM

This is the best I've come up with
Code:

CROP=crop=976:720:156:0
LAST3=${CROP#*:}
LAST2=${LAST3#*:}
THIRD=${LAST2%:*}
FOURTH=${LAST2#*:}

echo $THIRD $FOURTH
156 0

TOTAL=$(($THIRD + $FOURTH))
echo $TOTAL
156

So if there are black borders then TOTAL will not be zero

If there are no black borders then TOTAL will be zero

Is there a neater/more concise way?

shruggy 05-02-2021 10:16 AM

If you are getting the crop parameter with awk anyway, why not extract the sum of two values immediately as well?
Code:

<<! read CROP TOTAL
$(awk '/crop=/ {
    split($14, a, /:/)
    print $14, a[3]+a[4]
  }' CROPDATA.txt)
!

or in Bash
Code:

read CROP TOTAL < <(
  awk '/crop=/ {
    split($14, a, /:/)
    print $14, a[3]+a[4]
  }' CROPDATA.txt)


GPGAgent 05-02-2021 10:30 AM

Quote:

Originally Posted by shruggy (Post 6247232)
If you are getting the crop parameter with awk anyway, why not extract the sum of two values immediately as well?
Code:

<<! read CROP TOTAL
$(awk '/crop=/ {
    split($14, a, ":")
    print $14, a[3]+a[4]
  }' CROPDATA.txt)
!


Cheers Shruggy, I just don't know awk that well, I did look and this is exactly how I thought to do it - thanks

GPGAgent 05-02-2021 11:37 AM

Hey Shruggy,

So I included your code like this:
Code:

<<! read CROP TOTAL
TOTAL=$(awk '/crop=/ {
    split($14, a, ":")
    print $14, a[3]+a[4]
  }' CROPDATA.txt)
!

Could you explain what <<! read CROP TOTAL and the last ! do please
Cheers

shruggy 05-02-2021 11:56 AM

See 3.6.6 Here Documents in the GNU Bash Manual.

The usual way to write them is
Code:

read CROP TOTAL <<END
TOTAL=$(awk '/crop=/ {
    split($14, a, /:/)
    print $14, a[3]+a[4]
  }' CROPDATA.txt)
END

Here the output of awk is read into two variables: CROP and TOTAL. In Bash, you can achieve the same with the here string (<<<) or with the process redirection.

GPGAgent 05-02-2021 12:01 PM

Quote:

Originally Posted by shruggy (Post 6247271)
See 3.6.6 Here Documents in the GNU Bash Manual.

The usual way to write them is
Code:

read CROP TOTAL <<END
TOTAL=$(awk '/crop=/ {
    split($14, a, /:/)
    print $14, a[3]+a[4]
  }' CROPDATA.txt)
END

Here the output of awk is read into two variables: CROP and TOTAL. In Bash you can achieve the same with the here string (<<<) or with the process redirection.

Many thanks, everyday is a school day, that was very helpful


All times are GMT -5. The time now is 01:27 PM.