使用 bash 中的 grep 命令从文本文件中提取数据时出错

使用 bash 中的 grep 命令从文本文件中提取数据时出错

我使用 ffmpeg 生成了一个名为“silence.txt”的输出文件。当我尝试提取包含单词“silencedetect”的行时,我得到了一个奇怪的摘录。

这是我的原始“silence.txt”文件:

ffmpeg version 2.6.3 Copyright (c) 2000-2015 the FFmpeg developers
  built with Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.6.3 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-libfreetype --enable-libtheora --enable-libvorbis --enable-libvpx --enable-librtmp --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libass --enable-ffplay --enable-libspeex --enable-libschroedinger --enable-libfdk-aac --enable-libopus --enable-frei0r --enable-libopenjpeg --disable-decoder=jpeg2000 --extra-cflags='-I/usr/local/Cellar/openjpeg/1.5.1_1/include/openjpeg-1.5 ' --enable-nonfree --enable-vda
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test4.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 0
    compatible_brands: mp41avc1
    creation_time   : 2015-06-20 22:41:18
    playback_requirements: QuickTime 6.0 or greater
    playback_requirements-eng: QuickTime 6.0 or greater
    encoder         : vlc 2.2.1 stream output
    encoder-eng     : vlc 2.2.1 stream output
  Duration: 00:05:25.93, start: 0.000000, bitrate: 1761 kb/s
    Stream #0:0(fra): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2015-06-20 22:41:18
      handler_name    : SoundHandler
    Stream #0:1(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 720x576 [SAR 64:45 DAR 16:9], 1625 kb/s, 24.96 fps, 25 tbr, 1000k tbn, 50 tbc (default)
    Metadata:
      creation_time   : 2015-06-20 22:41:18
      handler_name    : VideoHandler
Output #0, null, to 'pipe:':
  Metadata:
    major_brand     : isom
    minor_version   : 0
    compatible_brands: mp41avc1
    encoder         : Lavf56.25.101
    playback_requirements: QuickTime 6.0 or greater
    playback_requirements-eng: QuickTime 6.0 or greater
    Stream #0:0(eng): Video: rawvideo (I420 / 0x30323449), yuv420p, 720x576 [SAR 64:45 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc (default)
    Metadata:
      creation_time   : 2015-06-20 22:41:18
      handler_name    : VideoHandler
      encoder         : Lavc56.26.100 rawvideo
    Stream #0:1(fra): Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s (default)
    Metadata:
      creation_time   : 2015-06-20 22:41:18
      handler_name    : SoundHandler
      encoder         : Lavc56.26.100 pcm_s16le
Stream mapping:
  Stream #0:1 -> #0:0 (h264 (native) -> rawvideo (native))
  Stream #0:0 -> #0:1 (aac (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 993 >= 993
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 5602 >= 5602
frame=  139 fps=0.0 q=0.0 size=N/A time=00:00:07.03 bitrate=N/A    
frame=  316 fps=311 q=0.0 size=N/A time=00:00:14.11 bitrate=N/A    
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 734723 >= 734723
[silencedetect @ 0x7feb4b40d9e0] silence_start: 15.056
[silencedetect @ 0x7feb4b40d9e0] silence_end: 15.6482 | silence_duration: 0.592229
frame=  488 fps=321 q=0.0 size=N/A time=00:00:21.04 bitrate=N/A    
frame=  631 fps=312 q=0.0 size=N/A time=00:00:26.75 bitrate=N/A    
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 1476678 >= 1476678
[silencedetect @ 0x7feb4b40d9e0] silence_start: 30.5347
[silencedetect @ 0x7feb4b40d9e0] silence_end: 31.0626 | silence_duration: 0.527958
frame=  839 fps=333 q=0.0 size=N/A time=00:00:34.08 bitrate=N/A    
frame=  975 fps=322 q=0.0 size=N/A time=00:00:39.52 bitrate=N/A    
frame= 1150 fps=325 q=0.0 size=N/A time=00:00:46.52 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 54.051
[silencedetect @ 0x7feb4b40d9e0] silence_end: 54.5362 | silence_duration: 0.485292
frame= 1366 fps=339 q=0.0 size=N/A time=00:00:55.16 bitrate=N/A    
frame= 1505 fps=332 q=0.0 size=N/A time=00:01:00.72 bitrate=N/A    
frame= 1645 fps=327 q=0.0 size=N/A time=00:01:06.32 bitrate=N/A    
frame= 1780 fps=321 q=0.0 size=N/A time=00:01:11.72 bitrate=N/A    
frame= 1957 fps=324 q=0.0 size=N/A time=00:01:18.80 bitrate=N/A    
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 4067886 >= 4067886
[silencedetect @ 0x7feb4b40d9e0] silence_start: 84.4542
[silencedetect @ 0x7feb4b40d9e0] silence_end: 85.4725 | silence_duration: 1.01831
frame= 2150 fps=329 q=0.0 size=N/A time=00:01:26.52 bitrate=N/A    
frame= 2289 fps=325 q=0.0 size=N/A time=00:01:32.08 bitrate=N/A    
frame= 2460 fps=326 q=0.0 size=N/A time=00:01:38.92 bitrate=N/A    
frame= 2584 fps=321 q=0.0 size=N/A time=00:01:43.88 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 104.986
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 5058544 >= 5058544
[silencedetect @ 0x7feb4b40d9e0] silence_end: 105.482 | silence_duration: 0.495979
frame= 2763 fps=323 q=0.0 size=N/A time=00:01:51.04 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 115.327
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 5557968 >= 5557968
[silencedetect @ 0x7feb4b40d9e0] silence_end: 115.983 | silence_duration: 0.656042
frame= 2939 fps=324 q=0.0 size=N/A time=00:01:58.08 bitrate=N/A    
frame= 3101 fps=324 q=0.0 size=N/A time=00:02:04.56 bitrate=N/A    
frame= 3264 fps=324 q=0.0 size=N/A time=00:02:11.08 bitrate=N/A    
frame= 3474 fps=329 q=0.0 size=N/A time=00:02:19.48 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 138.695
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 6679615 >= 6679615
[silencedetect @ 0x7feb4b40d9e0] silence_end: 139.415 | silence_duration: 0.719875
frame= 3636 fps=328 q=0.0 size=N/A time=00:02:25.96 bitrate=N/A    
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 7178004 >= 7178004
[silencedetect @ 0x7feb4b40d9e0] silence_start: 149.206
[silencedetect @ 0x7feb4b40d9e0] silence_end: 149.883 | silence_duration: 0.677187
frame= 3811 fps=329 q=0.0 size=N/A time=00:02:32.96 bitrate=N/A    
frame= 3968 fps=329 q=0.0 size=N/A time=00:02:39.24 bitrate=N/A    
frame= 4132 fps=329 q=0.0 size=N/A time=00:02:45.80 bitrate=N/A    
frame= 4296 fps=329 q=0.0 size=N/A time=00:02:52.36 bitrate=N/A    
frame= 4492 fps=331 q=0.0 size=N/A time=00:03:00.28 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 179.842
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 8652662 >= 8652662
[silencedetect @ 0x7feb4b40d9e0] silence_end: 180.44 | silence_duration: 0.597313
frame= 4655 fps=331 q=0.0 size=N/A time=00:03:06.80 bitrate=N/A    
frame= 4821 fps=331 q=0.0 size=N/A time=00:03:13.44 bitrate=N/A    
frame= 4985 fps=330 q=0.0 size=N/A time=00:03:20.00 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 205.278
[silencedetect @ 0x7feb4b40d9e0] silence_end: 205.806 | silence_duration: 0.527937
frame= 5167 fps=332 q=0.0 size=N/A time=00:03:27.28 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 206.216
[silencedetect @ 0x7feb4b40d9e0] silence_end: 206.659 | silence_duration: 0.442667
frame= 5339 fps=332 q=0.0 size=N/A time=00:03:34.16 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 215.789
[silencedetect @ 0x7feb4b40d9e0] silence_end: 216.274 | silence_duration: 0.485271
frame= 5539 fps=334 q=0.0 size=N/A time=00:03:42.16 bitrate=N/A    
frame= 5708 fps=334 q=0.0 size=N/A time=00:03:48.92 bitrate=N/A    
frame= 5870 fps=334 q=0.0 size=N/A time=00:03:55.40 bitrate=N/A    
frame= 6051 fps=334 q=0.0 size=N/A time=00:04:02.64 bitrate=N/A    
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 11837448 >= 11837448
[silencedetect @ 0x7feb4b40d9e0] silence_start: 246.309
[silencedetect @ 0x7feb4b40d9e0] silence_end: 246.742 | silence_duration: 0.432
frame= 6232 fps=335 q=0.0 size=N/A time=00:04:09.88 bitrate=N/A    
frame= 6385 fps=334 q=0.0 size=N/A time=00:04:16.00 bitrate=N/A    
frame= 6540 fps=334 q=0.0 size=N/A time=00:04:22.20 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 266.596
[silencedetect @ 0x7feb4b40d9e0] silence_end: 267.209 | silence_duration: 0.613229
frame= 6740 fps=335 q=0.0 size=N/A time=00:04:30.20 bitrate=N/A    
frame= 6908 fps=335 q=0.0 size=N/A time=00:04:36.92 bitrate=N/A    
frame= 7082 fps=336 q=0.0 size=N/A time=00:04:43.88 bitrate=N/A    
frame= 7243 fps=335 q=0.0 size=N/A time=00:04:50.32 bitrate=N/A    
frame= 7431 fps=336 q=0.0 size=N/A time=00:04:57.84 bitrate=N/A    
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 14273127 >= 14273127
[silencedetect @ 0x7feb4b40d9e0] silence_start: 297.191
[silencedetect @ 0x7feb4b40d9e0] silence_end: 297.677 | silence_duration: 0.485292
frame= 7600 fps=336 q=0.0 size=N/A time=00:05:04.60 bitrate=N/A    
frame= 7775 fps=336 q=0.0 size=N/A time=00:05:11.60 bitrate=N/A    
[null @ 0x7feb4b810600] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 15255582 >= 15255582
[silencedetect @ 0x7feb4b40d9e0] silence_start: 317.553
[silencedetect @ 0x7feb4b40d9e0] silence_end: 318.144 | silence_duration: 0.591896
frame= 7980 fps=338 q=0.0 size=N/A time=00:05:19.80 bitrate=N/A    
frame= 8135 fps=338 q=0.0 Lsize=N/A time=00:05:26.08 bitrate=N/A    
video:763kB audio:61036kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

我使用了以下 grep 命令...

grep silencedetect silence.txt > extract.txt

...获取此“extract.txt”文件:

[silencedetect @ 0x7feb4b40d9e0] silence_start: 15.056
[silencedetect @ 0x7feb4b40d9e0] silence_end: 15.6482 | silence_duration: 0.592229
[silencedetect @ 0x7feb4b40d9e0] silence_start: 30.5347
[silencedetect @ 0x7feb4b40d9e0] silence_end: 31.0626 | silence_duration: 0.527958
frame=  839 fps=333 q=0.0 size=N/A time=00:00:34.08 bitrate=N/A    
frame=  975 fps=322 q=0.0 size=N/A time=00:00:39.52 bitrate=N/A    
frame= 1150 fps=325 q=0.0 size=N/A time=00:00:46.52 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 54.051
[silencedetect @ 0x7feb4b40d9e0] silence_end: 54.5362 | silence_duration: 0.485292
[silencedetect @ 0x7feb4b40d9e0] silence_start: 84.4542
[silencedetect @ 0x7feb4b40d9e0] silence_end: 85.4725 | silence_duration: 1.01831
frame= 2150 fps=329 q=0.0 size=N/A time=00:01:26.52 bitrate=N/A    
frame= 2289 fps=325 q=0.0 size=N/A time=00:01:32.08 bitrate=N/A    
frame= 2460 fps=326 q=0.0 size=N/A time=00:01:38.92 bitrate=N/A    
frame= 2584 fps=321 q=0.0 size=N/A time=00:01:43.88 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 104.986
[silencedetect @ 0x7feb4b40d9e0] silence_end: 105.482 | silence_duration: 0.495979
frame= 2763 fps=323 q=0.0 size=N/A time=00:01:51.04 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 115.327
[silencedetect @ 0x7feb4b40d9e0] silence_end: 115.983 | silence_duration: 0.656042
frame= 2939 fps=324 q=0.0 size=N/A time=00:01:58.08 bitrate=N/A    
frame= 3101 fps=324 q=0.0 size=N/A time=00:02:04.56 bitrate=N/A    
frame= 3264 fps=324 q=0.0 size=N/A time=00:02:11.08 bitrate=N/A    
frame= 3474 fps=329 q=0.0 size=N/A time=00:02:19.48 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 138.695
[silencedetect @ 0x7feb4b40d9e0] silence_end: 139.415 | silence_duration: 0.719875
[silencedetect @ 0x7feb4b40d9e0] silence_start: 149.206
[silencedetect @ 0x7feb4b40d9e0] silence_end: 149.883 | silence_duration: 0.677187
frame= 3811 fps=329 q=0.0 size=N/A time=00:02:32.96 bitrate=N/A    
frame= 3968 fps=329 q=0.0 size=N/A time=00:02:39.24 bitrate=N/A    
frame= 4132 fps=329 q=0.0 size=N/A time=00:02:45.80 bitrate=N/A    
frame= 4296 fps=329 q=0.0 size=N/A time=00:02:52.36 bitrate=N/A    
frame= 4492 fps=331 q=0.0 size=N/A time=00:03:00.28 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 179.842
[silencedetect @ 0x7feb4b40d9e0] silence_end: 180.44 | silence_duration: 0.597313
frame= 4655 fps=331 q=0.0 size=N/A time=00:03:06.80 bitrate=N/A    
frame= 4821 fps=331 q=0.0 size=N/A time=00:03:13.44 bitrate=N/A    
frame= 4985 fps=330 q=0.0 size=N/A time=00:03:20.00 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 205.278
[silencedetect @ 0x7feb4b40d9e0] silence_end: 205.806 | silence_duration: 0.527937
frame= 5167 fps=332 q=0.0 size=N/A time=00:03:27.28 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 206.216
[silencedetect @ 0x7feb4b40d9e0] silence_end: 206.659 | silence_duration: 0.442667
frame= 5339 fps=332 q=0.0 size=N/A time=00:03:34.16 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 215.789
[silencedetect @ 0x7feb4b40d9e0] silence_end: 216.274 | silence_duration: 0.485271
[silencedetect @ 0x7feb4b40d9e0] silence_start: 246.309
[silencedetect @ 0x7feb4b40d9e0] silence_end: 246.742 | silence_duration: 0.432
frame= 6232 fps=335 q=0.0 size=N/A time=00:04:09.88 bitrate=N/A    
frame= 6385 fps=334 q=0.0 size=N/A time=00:04:16.00 bitrate=N/A    
frame= 6540 fps=334 q=0.0 size=N/A time=00:04:22.20 bitrate=N/A    
[silencedetect @ 0x7feb4b40d9e0] silence_start: 266.596
[silencedetect @ 0x7feb4b40d9e0] silence_end: 267.209 | silence_duration: 0.613229
[silencedetect @ 0x7feb4b40d9e0] silence_start: 297.191
[silencedetect @ 0x7feb4b40d9e0] silence_end: 297.677 | silence_duration: 0.485292
[silencedetect @ 0x7feb4b40d9e0] silence_start: 317.553
[silencedetect @ 0x7feb4b40d9e0] silence_end: 318.144 | silence_duration: 0.591896

问题是它给我的信息比我要求的要多,例如“frame=4655 fps=331 q=0.0 size=N/A time=00:03:06.80 bitrate=N/A”等等...

所以我的问题是如何使用 grep 命令获取我想要的行?接下来,如果您能给我更多帮助,我的想法是获取每个 silent_start 和 silent_end 值以使用 ffmpeg 启动另一个命令并将视频分成几个片段。

答案1

如果你将文件通过

cat -vet

您可以看到任何特殊字符。在这种情况下,您有回车符(如^M输出所示),它用于分隔行而不是换行符。例如:

...  bitrate=N/A    ^M[silencedetect ...

您可以将它们转换为换行符,然后使用 awk 提取时间,例如:

tr '\r' '\n' < silence4.txt |
awk '
/silence_start/{ start = $5 }
/silence_end/ { end = $5; printf "%s to %s\n",start,end }'

答案2

我会尝试

grep silencedetect silence.txt | cat -n -v

或者

grep silencedetect silence.txt | less -N -S

或者

grep silencedetect silence.txt | hexdump -C

因为看起来多余的线条可能

  • 只需将匹配的行结尾换行即可。
  • 包含模糊匹配的控制字符的行(例如 CR)。

对于你问题的第二部分

egrep 'silencedetect.*silence_(start|end)' silence.txt

或等效的awkperl以便您可以捕获时间并发出 ffmpeg 命令。

答案3

我在我的 Mac 上尝试过,它似乎按预期工作。我怀疑grep你拥有的命令可能通过别名或路径中第一个出现的另一个命令被更改。试试这个:

/usr/bin/grep silence.txt  # On Mac
/bin/grep silence.txt      # On Linux

看看是否得到了期望的输出。

相关内容