【问题标题】:Pipe and regex bash ouput管道和正则表达式 bash 输出
【发布时间】:2013-02-28 03:37:14
【问题描述】:

我正在尝试使用 ffmpeg 获取视频的尺寸。我使用命令ffprobe video.mov获取以下数据:

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。我该怎么办?

【问题讨论】:

    标签: macos bash ffmpeg terminal zsh


    【解决方案1】:

    您可以通过 2 打方法来做到这一点。这是使用grep 和awk 的方法:

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

    【讨论】:

    • grep | awk 是对 grep 的无用使用。更好地使用awk '/Stream/{print $10}'
    • @Wes 有趣; ffprobe 必须写信给stderr 而不是stdout。试试:ffprobe video.mov 2>&1 | grep ....
    • @sputnick Ack 在匹配方面比 grep 慢得多。
    【解决方案2】:

    试试这个:

    $ 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分隔会更可靠

    【讨论】:

    • Hrm,看来这两个仍然给我来自终端中ffprobe 的所有信息。我的管道正确吗?它将 grep 用法附加到所有内容的顶部
    猜你喜欢
    • 2012-04-13
    • 2021-11-27
    • 2021-09-03
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    相关资源
    最近更新 更多