2013-03-12 64 views
1

我試圖用ffmpeg來獲取視頻的尺寸。我用命令ffprobe video.mov得到以下數據:管道和正則表達式bash輸入

ffprobe version 1.0 Copyright (c) 2007-2012 the FFmpeg developers 
    built on Jan 14 2013 10:18:07 with Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn) 
    configuration: --prefix=/usr/local/Cellar/ffmpeg/1.0 --enable-shared --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --cc=cc --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid 
    libavutil  51. 73.101/51. 73.101 
    libavcodec  54. 59.100/54. 59.100 
    libavformat 54. 29.104/54. 29.104 
    libavdevice 54. 2.101/54. 2.101 
    libavfilter  3. 17.100/3. 17.100 
    libswscale  2. 1.101/2. 1.101 
    libswresample 0. 15.100/0. 15.100 
    libpostproc 52. 0.100/52. 0.100 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'linebubble.mov': 
    Metadata: 
    major_brand  : qt 
    minor_version : 0 
    compatible_brands: qt 
    creation_time : 2013-03-12 02:59:34 
    Duration: 00:00:14.05, start: 0.050000, bitrate: 135 kb/s 
    Stream #0:0(und): Video: h264 (Main) (avc1/0x31637661), yuv420p, 502x288 [SAR 1:1 DAR 251:144], 137 kb/s, 7.84 fps, 60 tbr, 6k tbn, 12k tbc 
    Metadata: 
     creation_time : 2013-03-12 02:59:34 
     handler_name : Core Media Data Handler 

現在我要管的是成正則表達式只得到502x288在第四最後一行。

我已經看到了一些python和perl的解決方案,但我希望這是純粹的bash。我將如何去做這件事?

回答

0

你可以做2打方法。下面是使用grepawk一個遠:

ffprobe video.mov 2>&1 | grep 'Stream #0' | awk '{print $10}' 
+1

'grep | awk'是gr​​ep的無用之處。更好地使用'awk'/ Stream/{print $ 10}'' – 2013-03-12 14:49:55

+0

@sputnick是的,這也可以。 – trojanfoe 2013-03-12 14:50:17

+0

這仍然顯示我沒有過濾任何它的所有信息http://wes.io/NVsY – wesbos 2013-03-12 15:59:51

0

試試這個:

$ ffprobe video.mov 2>&1 | grep -oP '^\s+Stream.*?,\s+\K\d{2,}x\d{2,}' 
502x288 

如果你沒有grep -oP交換機:

$ ffprobe video.mov 2>&1 | 
    perl -lne 'print $& if /^\s+Stream.*?,\s+\K\d{2,}x\d{2,}/' 
502x288 

這個解決方案不是基於第N列的,我認爲這是更可靠的搜索一個逗號,後跟由空格後跟2個整數至少2位數字隔開x

+0

HRM,似乎這兩個還是給我所有從termina'ffprobe'的信息。我的管道是否正確?它將grep用法添加到所有內容的頂端 – wesbos 2013-03-12 14:52:56

+0

查看我編輯的帖子 – 2013-03-12 14:54:11

+0

第一個,我仍然使用grep:http://wes.io/NVyf – wesbos 2013-03-12 14:56:09