[英]Saving different values with same title into different variables (BASH)
考慮這個輸出
General
Complete name : sample.mp4
Format : MPEG-4
Format profile : QuickTime
Codec ID : qt
File size : 37.3 MiB
Duration : 9mn 56s
Overall bit rate mode : Variable
Overall bit rate : 525 Kbps
Writing application : Lavf52.73.0
Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : Baseline@L3.0
Format settings, CABAC : No
Format settings, ReFrames : 3 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 9mn 56s
Bit rate : 400 Kbps
Width : 424 pixels
Height : 240 pixels
Display aspect ratio : 16:9
Frame rate mode : Constant
Frame rate : 24.000 fps
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.164
Stream size : 29.9 MiB (80%)
Writing library : x264 core 98 r1629 9d1c441
Encoding settings : cabac=0 / ref=3 / deblock=1:0:0 / analyse=0x1:0x111 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=64 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / constrained_intra=0 / bframes=0 / weightp=0 / keyint=90 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=abr / mbtree=1 / bitrate=400 / ratetol=10.0 / qcomp=0.60 / qpmin=10 / qpmax=51 / qpstep=4 / vbv_maxrate=768 / vbv_bufsize=3000 / ip_ratio=1.41 / aq=1:1.00 / nal_hrd=none
Language : English
Audio
ID : 2
Format : AAC
Format/Info : Advanced Audio Codec
Format profile : LC
Codec ID : 40
Duration : 9mn 56s
Bit rate mode : Variable
Bit rate : 99.1 Kbps
Channel(s) : 2 channels
Channel positions : Front: L R
Sampling rate : 48.0 KHz
Compression mode : Lossy
Stream size : 7.04 MiB (19%)
Language : English
這是顯示媒體信息的命令的示例輸出。 這包含標題視頻下的比特率和標題音頻下的另一個比特率。 我的目標是將這兩個存儲為BASH腳本中的不同變量。
不能使用行號,因為有些文件可能有音頻部分而有些文件沒有。 那么,有沒有方法將這兩個同名的值保存到不同的變量中?
您可以使用sed分別打印視頻和音頻部分,然后使用另一個sed提取比特率。 我們假設信息在一個文件中。
VIDEO=`sed -n '/^Video$/,/^$/p' file`
AUDIO=`sed -n '/^Audio$/,/^$/p' file`
VBR=`echo $VIDEO | sed -nr 's/^Bit rate +: //p'`
ABR=`echo $AUDIO | sed -nr 's/^Bit rate +: //p'`
至於提取視頻和音頻章節:
sed -n
選項意味着無打印 /.../,/.../
我定義一個起始正則表達式和一個結束正則表達式 /^Audio$/
表示整行只包含“音頻” /^$/
表示空行 p
命令打印上面定義的范圍(覆蓋范圍的-n) 如果AUDIO
的輸出始終在視頻之后,則以下內容將起作用:
grep 'Bit rate[ ]*:' t|sed -n '1p'|cut -f2 -d':'|sed 's/^ //g'
用於視頻和
grep 'Bit rate[ ]*:' t|sed -n '1p'|cut -f2 -d':'|sed 's/^ //g'
用於音頻。
aman@a1:~$ VIDEO=$(grep 'Bit rate[ ]*:' t|sed -n '1p'|cut -f2 -d':'|sed 's/^ //g')
aman@a1:~$ AUDIO=$(grep 'Bit rate[ ]*:' t|sed -n '2p'|cut -f2 -d':'|sed 's/^ //g')
aman@a1:~$ echo $VIDEO
400 Kbps
aman@a1:~$ echo $AUDIO
99.1 Kbps
使用sed過濾視頻行然后grep速率並存儲在video_bitrate中。 對音頻做同樣的事情。
編輯:Raul Andres使用awk同時提出的完全相同的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.