LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2018, 06:20 PM   #1
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Rep: Reputation: Disabled
How can I use regex to extract video bitrate


Hi folks,

I am trying to get the bitrate of a video to use in a variable. This is the best I can do with my limited knowledge of regex.

Code:
 x=$(ffmpeg -i  lt.mp4 2>&1| grep DAR | awk -F, '{print $5 }' | grep -P "\d" | awk '{print $1 }')
% echo $x
1261
Is there a better or cleaner way to do this? Thanks
 
Old 05-12-2018, 06:34 PM   #2
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
I tried another command. So far, this is the cleaner way

Code:
vb=$(mplayer -identify -vo dummy -ao dummy lt.mp4 2>/dev/null| grep "VIDEO:" | awk '{print $7 }')
echo $vb
1261.8
If you guys know a better way or command. I would appreciate it. Thanks
 
Old 05-12-2018, 07:02 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,155

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
awk has all you need - you should never need to pipe grep or similar to it.
Code:
awk '/VIDEO:/ {print $7 }'
 
2 members found this post helpful.
Old 05-12-2018, 07:15 PM   #4
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
awk has all you need - you should never need to pipe grep or similar to it.
Code:
awk '/VIDEO:/ {print $7 }'
That's awesome syg00, thanks

+1
 
Old 05-12-2018, 09:12 PM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Assuming the file actually reports the bit rate for video.
What happens when it doesn't report it, but reports the total bitrate?
Code:
  Duration: 00:00:57.83, start: 1.400000, bitrate: 571 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 412x320 [SAR 7520:7519 DAR 94:73], 30 fps, 30 tbr, 90k tbn, 60 tbc
If you know what you're working with, syg00's answer will be fine, but if you're working with a variety of unknown files you may need to look at edge cases, figuring out the video bit-rate by deducting audio bitrate etc.

Reference
https://superuser.com/questions/1106...e-using-ffmpeg

Last edited by Sefyir; 05-12-2018 at 09:15 PM.
 
Old 05-12-2018, 09:41 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,155

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Too true - but life would be pretty bland without edge cases ....
 
Old 05-12-2018, 10:10 PM   #7
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sefyir View Post
Assuming the file actually reports the bit rate for video.
What happens when it doesn't report it, but reports the total bitrate?
Code:
  Duration: 00:00:57.83, start: 1.400000, bitrate: 571 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 412x320 [SAR 7520:7519 DAR 94:73], 30 fps, 30 tbr, 90k tbn, 60 tbc
If you know what you're working with, syg00's answer will be fine, but if you're working with a variety of unknown files you may need to look at edge cases, figuring out the video bit-rate by deducting audio bitrate etc.

Reference
https://superuser.com/questions/1106...e-using-ffmpeg
If this is the case, then I would just use some arbitrary value between 2K to 5k to encode a video. These bitrates would yield good video quality depending on the resolution of the video.

 
  


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
Regex to extract data between html tag oulevon Programming 6 11-05-2017 04:42 AM
Application to download web pages and extract bits through regex? littlebigman Linux - Software 5 05-14-2015 08:18 AM
Python (ideally 3) - library to extract info such as bitrate from audio files sycamorex Programming 2 12-04-2013 01:01 PM
Extract part of a string based on regex winairmvs Linux - Software 5 02-14-2011 12:56 PM
Extract everything before a regex match onesikgypo Programming 5 10-21-2009 04:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:35 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