[SOLVED] Using ffprobe - getting specific information for use in a script
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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
#
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
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.