ffmpeg获取视频时间长度的方法

2021年12月31日

在我们日常的工作中,有时候我们需要获取一段视频的长度,但却不知道如何获取,其实这里要拿出ffmpeg了,原理非常简单,在使用ffmpeg调入一个视频的时候,ffmpeg会显示出这个视频的所有信息,我们只需要根据不同字段去截取不同的字符串,就可以获得这些相关信息了。

例如获取视频的总时间长。

Windows PowerShell
版权所有 (C) 2009 Microsoft Corporation。保留所有权利。

PS C:\Users\Administrator> cd D:\
PS D:\> D:\ffmpeg\bin\ffmpeg.exe -i D:\video.mp4
ffmpeg version N-102564-g2261cc6d8a Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10-win32 (GCC) 20210408
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-
mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-
pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lz
ma --enable-fontconfig --enable-libvorbis --enable-opencl --enable-libvmaf --enable-vulkan --enable-amf --enable-libaom
--enable-avisynth --enable-libdav1d --enable-libdavs2 --enable-ffnvcodec --enable-cuda-llvm --enable-libglslang --enable
-libgme --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --ena
ble-libwebp --enable-lv2 --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --en
able-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1
 --enable-libtwolame --enable-libuavs3d --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxavs2 --enable
-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexefla
gs= --extra-libs=-lgomp
  libavutil      57.  0.100 / 57.  0.100
  libavcodec     59.  1.100 / 59.  1.100
  libavformat    59.  2.100 / 59.  2.100
  libavdevice    59.  0.100 / 59.  0.100
  libavfilter     8.  0.101 /  8.  0.101
  libswscale      6.  0.100 /  6.  0.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\video.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf59.2.100
  Duration: 00:21:52.75, start: 0.000000, bitrate: 747 kb/s
  Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 854x480 [SAR 1:1 DAR 427:240], 614 kb/s, 25 fps, 25
 tbr, 12800 tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 126 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
At least one output file must be specified
PS D:\> Duration:

在这里我用ffmpeg打开了video.mp4,上边就是ffmpeg返回的这个视频的一些信息。我们只需要获取“Duration: 00:21:52.75, start: 0.000000, bitrate: 747 kb/s” 这一段字段,就可以获取到视频总时长了。

ffmpeg -i D:/download/test.mkv 2>&1 | grep 'Duration' | cut -d ' ' -f 4

我们使用grep来寻找Duration这个字段,然后用cut分割字符,就可以获取到视频总时间长度了。

 


sicnature ---------------------------------------------------------------------
Your current IP address is: 52.91.67.23
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://www.myzhenai.com/post/4017.html

2条评论

  • 飞鱼先生 2022年01月28日在10:28 下午

    可以多写点生活呢

    • 海南胡说 2022年02月10日在8:52 上午

      谢谢您的回复,我这个不是生活类博客

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注