LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-06-2019, 09:54 AM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Question Using ffprobe - getting specific information for use in a script


I would like to extract the video bitrate from a command and use that result in a script

ffprobe gives me the infomation:
Code:
charlie@charlie-machine:/media/charlie/TEST$ ffprobe piano.avi 
ffprobe version 1.2.6 Copyright (c) 2007-2014 the FFmpeg developers
  built on May  2 2019 18:57:27 with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)
  configuration: --enable-libx264 --enable-gpl
  libavutil      52. 18.100 / 52. 18.100
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.104 / 54. 63.104
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 42.103 /  3. 42.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Input #0, avi, from 'piano.avi':
  Metadata:
    encoder         : Lavf51.12.1
  Duration: 00:56:04.06, start: 0.000000, bitrate: 1443 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (xvid / 0x64697678), yuv420p, 456x304 [SAR 1:1 DAR 3:2], 29.97 tbr, 29.97 tbn, 29.97 tbc
    Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16p, 128 kb/s
charlie@charlie-machine:/media/charlie/IOMEGA_01/USENET/DVD2/TEST$
So how can I create a script so that I can use it like this:
Code:
charlie@charlie-machine:/media/charlie/TEST$ VBR.sh piano.avi
1443
charlie@charlie-machine:/media/charlie/TEST$
Cheers folks
 
Old 09-06-2019, 10:00 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Consider morphing the answer provided here to at least get the line, then the bitrate: term, then just the actual number. I think it'll take a few iterations. Sorry, not an awk expert, but this seems typical of how to approach this:

https://unix.stackexchange.com/quest...t-has-the-text

Or web search with the string "use bash script to search paragraph for a term"
 
Old 09-06-2019, 10:28 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
do you want the overall bitrate?
i.e. the Video + the auido?

probably the simplest way
Code:
#!/bin/bash
input="$1"
Bitrate="$(ffprobe -hide_banner "${input}" 2>&1 | grep -o bitrate.*[0-9] )" 
echo "${Bitrate}"
echo "${Bitrate#bitrate: }"
Bitrate="${Bitrate#bitrate: }"
echo "${Bitrate}"

# 
# The 2>&1  is redirecting stderr to stdout
# then you can use grep
# -o, --only-matching
#      show only nonempty parts of lines that match
#
 
1 members found this post helpful.
Old 09-06-2019, 10:35 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
now , there is another way

Code:
ffprobe -hide_banner -print_format json -show_format "${input}"
this will spit out json

you could go parsing that with grep/sed/awk

but jq is much easier

Code:
#!/bin/bash
input="$1"
Bitrate(){
ffprobe -hide_banner \
   -print_format json \
   -show_format "${input}" 2>/dev/null \
   | jq -r ".format.bit_rate"
}
Bitrate

Note , the bitrate in that is bits and not kb !!

Last edited by Firerat; 09-06-2019 at 10:36 AM.
 
Old 09-06-2019, 10:40 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
another??

mediainfo

Code:
mediainfo --Output="General;%OverallBitRate%" "${input}"
you can get a massive list
Code:
mediainfo --Info-Parameters
 
1 members found this post helpful.
Old 09-06-2019, 11:09 AM   #6
HappyTux
Senior Member
 
Registered: Mar 2003
Location: Nova Scotia, Canada
Distribution: Debian AMD64
Posts: 4,170

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by GPGAgent View Post
I would like to extract the video bitrate from a command and use that result in a script

ffprobe gives me the infomation:
Code:
charlie@charlie-machine:/media/charlie/TEST$ ffprobe piano.avi 
ffprobe version 1.2.6 Copyright (c) 2007-2014 the FFmpeg developers
  built on May  2 2019 18:57:27 with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)
  configuration: --enable-libx264 --enable-gpl
  libavutil      52. 18.100 / 52. 18.100
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.104 / 54. 63.104
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 42.103 /  3. 42.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Input #0, avi, from 'piano.avi':
  Metadata:
    encoder         : Lavf51.12.1
  Duration: 00:56:04.06, start: 0.000000, bitrate: 1443 kb/s
    Stream #0:0: Video: mpeg4 (Simple Profile) (xvid / 0x64697678), yuv420p, 456x304 [SAR 1:1 DAR 3:2], 29.97 tbr, 29.97 tbn, 29.97 tbc
    Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16p, 128 kb/s
charlie@charlie-machine:/media/charlie/IOMEGA_01/USENET/DVD2/TEST$
So how can I create a script so that I can use it like this:
Code:
charlie@charlie-machine:/media/charlie/TEST$ VBR.sh piano.avi
1443
charlie@charlie-machine:/media/charlie/TEST$
Cheers folks

Code:
#!/bin/bash
ffprobe $i &> temp.txt; cat temp.txt | grep bitrate | cut -d ":" -f 6 ; rm temp.txt
The result of the command run on a file locally.

Code:
MacUser2525:~$ ffprobe /Volumes/Sea_To_Do/watching/TV/Department_S_Series_01/Department_S-S01xE05-One_Of_Our_Aircraft_Is_Empty.mkv &> temp.txt; cat temp.txt | grep bitrate | cut -d ":" -f 6
 1105 kb/s
Or if wanting to use mediainfo as suggested in previous post, the script I have for getting my information on multiple files. I adapted the line above from one of them in it.


Code:
MacUser2525:~$ cat Bin/mediainfo_video_audio.sh 
#!/bin/bash

# Get directory script is running in
DIR="$(dirname "$PWD")/$(basename "$PWD")"

# Change to directory
cd $DIR

# Now process all .mkv files under that directory
for i in $(find $PWD -not -path '*/\.*' | sort  | grep .mkv ); do

	# Use mediainfo to get information needed
	/usr/local/bin/mediainfo "$i" &> /tmp/tmpinfo

	# Get channels in mediainfo created file used for audio mono/stereo/surround
	Title=$(cat /tmp/tmpinfo | grep -C 2 Duration | grep "Channel(s)" | cut -d ":" -f 2 | cut -d " " -f 2)
	
	# File processing according to channels found
	case $Title in
	
		1) 
			Title=Mono
		;;
		
		2)
			Title=Stereo
		;;
		
		6)
			Title=Surround
		;;
	esac
		
	# Get video width in mediainfo created file
	Width=$(grep Width /tmp/tmpinfo)
	
	# Get video height in mediainfo created file
	Height=$(grep Height /tmp/tmpinfo)
	
	# Get bit rate in mediainfo created file
	Bitrate=$(grep "Overall bit rate" /tmp/tmpinfo)
	
	# Get aspect ratio in mediainfo created file
	Aspect=$(grep "Display aspect ratio" /tmp/tmpinfo)
	
	# Get video codec in mediainfo created file
	Video=$(cat /tmp/tmpinfo | grep -C 3 Width | grep "Codec ID" | cut -d ":" -f 2)
	
	# Get audio codec in mediainfo created file
	Audio=$(cat /tmp/tmpinfo | grep -C 3 "Channel(s)" | grep "Codec ID" | cut -d ":" -f 2)
	
	# Get file size in mediainfo created file
	Size=$(grep "File size" /tmp/tmpinfo)

	# Get audio channels in mediainfo created file positions
	Channel=$(grep "Channel(s)" /tmp/tmpinfo)
	
	# Get audio channels positions in mediainfo created file 
	Channelpos=$(grep "Channel positions" /tmp/tmpinfo)
	
	# Get duration of video in mediainfo created file
	Duration=$(cat /tmp/tmpinfo | grep -C 1 "File size" | grep Duration)

	# Echo what was done
	echo $i
	echo "$Size"
	echo "$Duration"
	echo "$Bitrate"
	echo "Codec ID                                 :""$Video"
	echo "$Width"
	echo "$Height"
	echo "$Aspect"
	echo "Codec ID                                 :""$Audio"
	#echo "Audio                                    :" "$Title"
	echo "$Channel"
	echo "$Channelpos"
	echo " "

	# Remove temporary file created with mediainfo
	rm /tmp/tmpinfo

# Done processing all files
done
 
Old 09-06-2019, 11:28 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I don't get it

why would you
Code:
cat temp.txt | grep bitrate
?

why not
Code:
grep bitrate temp.txt
hell, why even have the temp.txt
Code:
ffprobe $i 2>&1 | grep bitrate | cut -d ":" -f 6

regards mediainfo and writing to a temp file

Code:
tempVar="$(mediaino --Output=JSON "$inputfile")"
you can then query that later with jq ( or your pipe trains )

Code:
<<<$tempVar jq .
Edit:
I'll throw this in aswell
mediainfo uses @ in some of its lables
I havn't figured out how to escape those with jq.. so

Code:
<<<${tempVar//@} jq .
which simply deletes all the @

Last edited by Firerat; 09-06-2019 at 11:48 AM.
 
2 members found this post helpful.
Old 09-06-2019, 11:48 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Did nobody consider that ffprobe might be able to do it all by itself?
https://video.stackexchange.com/a/16359
 
1 members found this post helpful.
Old 09-06-2019, 11:53 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by ondoho View Post
Did nobody consider that ffprobe might be able to do it all by itself?
https://video.stackexchange.com/a/16359
I just like json
 
Old 09-06-2019, 12:33 PM   #10
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by ondoho View Post
Did nobody consider that ffprobe might be able to do it all by itself?
https://video.stackexchange.com/a/16359
Good, but my avi file returns a bitrate=N/A
Code:
charlie@charlie-machine:/media/charlie/IOMEGA_01/USENET/DVD2/TEST$ ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1 piano.avi 
bit_rate=N/A
 
Old 09-06-2019, 12:40 PM   #11
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Cool

Quote:
Originally Posted by Firerat View Post
another??

mediainfo

Code:
mediainfo --Output="General;%OverallBitRate%" "${input}"
you can get a massive list
Code:
mediainfo --Info-Parameters
Cheers Firerat, this will do the job:
Code:
charlie@charlie-machine:/media/charlie/IOMEGA_01/USENET/DVD2/TEST$ bitrate=$(( $(mediainfo --Output="General;%OverallBitRate%" piano.avi) / 1000 ))
charlie@charlie-machine:/media/charlie/IOMEGA_01/USENET/DVD2/TEST$ echo $bitrate 
1443
So I'll add the above to my script and I have the bitrate to use in further processing - nice.

Thanks to everyone else that replied. All good stuff
 
Old 09-06-2019, 02:04 PM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
np

I used to use mediainfo a lot, not so much now.

mind if I ask what you are using the ABR for?

RE-Encoding ?
Personally I would use crf ( constant rate factor )
but maybe use the ABR to cap it ( so I defiantly don't get a larger file )

copy audio ( I would probably go with opus if size was a concern )
video as hevc
Code:
input="piano.avi"
output="piano.mkv"
br="$(( that long commnd ;)  ))"
ffmpeg -i "${input}" \
    -c:a copy \
    -c:v libx265 \
    -preset medium \
    -x265-params vbv-bufsize=${br}:vbv-maxrate=${br}:crf=24 \
  "${output}"
in bold, since the rate is capped at the same as the source ( well not quite *) I should not get a file larger

* we only capped the video bitrate , we got vid+audio bit rate
so it might end up slightly larger

I don't understand all the in and outs of ffmpeg encoding
I've read many a flame war where two or three ppl argue over this and that param.

What I have found is
preset , the faster it is the worse the quality and the *smaller* the file size

crf, the lower it is the better the quality, and the file is larger.


x265 ( hevc ) defaults are medium preset and crf of 28

if the source is bad quality / low resolution, I find a lower crf gets a better end result
Higher quality source , high res.. you can get away with a higher crf
 
1 members found this post helpful.
Old 09-07-2019, 01:20 AM   #13
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by GPGAgent View Post
Good, but my avi file returns a bitrate=N/A
Code:
charlie@charlie-machine:/media/charlie/IOMEGA_01/USENET/DVD2/TEST$ ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1 piano.avi 
bit_rate=N/A
try to remove "-select_streams v:0" from that command.
Also, while troubleshooting, remove "-v error".
 
1 members found this post helpful.
Old 09-08-2019, 05:31 PM   #14
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by Firerat View Post
np

I used to use mediainfo a lot, not so much now.

mind if I ask what you are using the ABR for?

RE-Encoding ?
Personally I would use crf ( constant rate factor )
but maybe use the ABR to cap it ( so I defiantly don't get a larger file )

copy audio ( I would probably go with opus if size was a concern )
video as hevc
Code:
input="piano.avi"
output="piano.mkv"
br="$(( that long commnd ;)  ))"
ffmpeg -i "${input}" \
    -c:a copy \
    -c:v libx265 \
    -preset medium \
    -x265-params vbv-bufsize=${br}:vbv-maxrate=${br}:crf=24 \
  "${output}"
in bold, since the rate is capped at the same as the source ( well not quite *) I should not get a file larger

* we only capped the video bitrate , we got vid+audio bit rate
so it might end up slightly larger

I don't understand all the in and outs of ffmpeg encoding
I've read many a flame war where two or three ppl argue over this and that param.

What I have found is
preset , the faster it is the worse the quality and the *smaller* the file size

crf, the lower it is the better the quality, and the file is larger.


x265 ( hevc ) defaults are medium preset and crf of 28

if the source is bad quality / low resolution, I find a lower crf gets a better end result
Higher quality source , high res.. you can get away with a higher crf
Hi Firerat, Yep I'm re-encoding a load of wmv's, avi's mph's and so on to mp4, and your tips will help loads.

I'm trying to maintain the size and aspect ratio and using a video bitrate of 1000K or if the original is less use that instead. Audio I'm just copying
 
Old 09-08-2019, 05:40 PM   #15
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by ondoho View Post
try to remove "-select_streams v:0" from that command.
Also, while troubleshooting, remove "-v error".
ondoho, Cheers that worked, almost:
Code:
ffprobe -v error -show_entries stream=bit_rate -of default=noprint_wrappers=1 piano.avi 
bit_rate=N/A
bit_rate=128000
But I get two bit_rate lines, how can I exclude the N/A one, and I guess this is the audio bitrate?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ffprobe to sort videos Entropy1024 Linux - Newbie 2 04-13-2018 08:50 AM
Why do some tools (e.g. ffprobe) split their output between sdtout and stderr ? markus-n Linux - General 6 01-07-2018 03:21 PM
Shell Script To Extract Specific Information From File immolation Programming 6 06-13-2013 12:27 PM
Use specific field in inode to store information? paragamritkar Linux - Kernel 3 09-28-2011 10:38 PM
How to extract metadata information using exiftool for specific model of camera??? confused.com Programming 1 10-17-2008 08:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration