使用ffmpeg命令进行视频格式转换

2023-12-13 12:28:15

1 ffmpeg介绍

FFmpeg 是一个非常强大和灵活的开源工具集,用于处理音频和视频文件。它提供了一系列的工具和库,可以用于录制、转换、流式传输和播放音频和视频。

FFmpeg 主要特点如下:

  • 格式支持广泛:FFmpeg 支持几乎所有的音频和视频格式,包括非常流行的格式如 MP4, AVI, MOV, MP3, AAC 等。

  • 转码和处理能力:它可以用来转换文件格式,调整视频质量和分辨率,提取音频,合并或分割视频文件等。

  • 流媒体功能:FFmpeg 可以用于实时视频流的捕获、编码和转码,非常适合直播应用。

  • 命令行工具:通过命令行界面,用户可以使用一系列的命令和参数来精确控制音视频处理过程。

  • 编程接口:FFmpeg 提供了丰富的编程接口,开发者可以使用它们来集成音视频处理功能到自己的应用中。

  • 社区和文档:有着活跃的开发社区和详细的文档,使得用户和开发者可以轻松地学习和使用 FFmpeg。

  • 跨平台兼容性:它可以在多种操作系统上运行,包括 Windows, macOS, Linux 等。

  • 免费和开源:FFmpeg 是完全免费和开源的,适用于个人和商业用途。

一个典型的视频转换处理过程:

2 ffmpeg使用场景

2.1 查看视频基本信息

ffmpeg -i video01.mp4 

显示结果如下:

ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7.3.0 (crosstool-NG 1.23.0.449-a04d0)
  configuration: --prefix=/tmp/build/80754af9/ffmpeg_1587154242452/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho --cc=/tmp/build/80754af9/ffmpeg_1587154242452/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc --disable-doc --enable-avresample --enable-gmp --enable-hardcoded-tables --enable-libfreetype --enable-libvpx --enable-pthreads --enable-libopus --enable-postproc --enable-pic --enable-pthreads --enable-shared --enable-static --enable-version3 --enable-zlib --enable-libmp3lame --disable-nonfree --enable-gpl --enable-gnutls --disable-openssl --enable-libopenh264 --enable-libx264
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video01.mp4':
   Metadata:
    major_brand     : XAVC
    minor_version   : 17506303
    compatible_brands: XAVCmp42iso6
    creation_time   : 2021-01-24T08:58:52.000000Z
  Duration: 00:06:22.56, start: 0.000000, bitrate: 305946 kb/s
  Stream #0:0[0x1](und): Video: h264 (High 4:2:2) (avc1 / 0x31637661), yuv422p10le(tv, bt709, progressive), 3840x2160 [SAR 1:1 DAR 16:9], 279989 kb/s, 100 fps, 100 tbr, 100k tbn (default)
    Metadata:
      creation_time   : 2021-01-24T08:58:52.000000Z
      handler_name    : Video Media Handler
      vendor_id       : [0][0][0][0]
      encoder         : AVC Coding
    Side data:
      displaymatrix: rotation of -90.00 degrees
  Stream #0:1[0x2](und): Audio: pcm_s16be (twos / 0x736F7774), 48000 Hz, 2 channels, s16, 1536 kb/s (default)
    Metadata:
      creation_time   : 2021-01-24T08:58:52.000000Z
      handler_name    : Sound Media Handler
      vendor_id       : [0][0][0][0]
  Stream #0:2[0x3](und): Data: none (rtmd / 0x646D7472), 23756 kb/s (default)
    Metadata:
      creation_time   : 2021-01-24T08:58:52.000000Z
      handler_name    : Timed Metadata Media Handler
      timecode        : 07:34:17:80
  • 视频分辨率 1920x1080

  • 时长:00:06:33.15

  • 帧率:25fps

  • 编码器:h264

  • 音频采样率:48000HZ

  • 音频编码:aac

2.2 调整视频码率和分辨率

ffmpeg -i video01.mp4 -vf scale=1080*1920 -r 25 processed_video01.mp4

查看转换后的视频信息:

ffmpeg -i processed_video01.mp4
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 7 (GCC)
  configuration: 
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'processed_video01.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf60.11.100
  Duration: 00:06:22.64, start: 0.000000, bitrate: 1038 kb/s
    Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p10le(tv, bt709), 1080x1920 [SAR 1:1 DAR 9:16], 903 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : Video Media Handler
      encoder         : Lavc60.26.100 libx264
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 129 kb/s (default)
    Metadata:
      handler_name    : Sound Media Handler

2.3 提取视频的第一帧

ffmpeg -i video01.mp4 -ss 00:00:01 -vframes 1 output.jpg

查看截取后的图片信息:

ffmpeg -i output.jpg
ffmpeg version 2023-09-07-git-9c9f48e7f2-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-dxva2 --enable-d3d11va --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
  libavutil      58. 19.100 / 58. 19.100
  libavcodec     60. 26.100 / 60. 26.100
  libavformat    60. 11.100 / 60. 11.100
  libavdevice    60.  2.101 / 60.  2.101
  libavfilter     9. 11.100 /  9. 11.100
  libswscale      7.  3.100 /  7.  3.100
  libswresample   4. 11.100 /  4. 11.100
  libpostproc    57.  2.100 / 57.  2.100
Input #0, image2, from 'output.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 14454 kb/s
  Stream #0:0: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 1080x1920 [SAR 1:1 DAR 9:16], 25 fps, 25 tbr, 25 tbn
At least one output file must be specified

2.4 截取第10帧以后得视频

ffmpeg -i processed_video01.mp4 -vf select="gte(n\, 10)" cut_video01.mp4

查看截取后的视频信息:

ffmpeg -i cut_video01.mp4 
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 7 (GCC)
  configuration: 
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cut_video01.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.45.100
  Duration: 00:06:22.64, start: 0.000000, bitrate: 1179 kb/s
    Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], 1046 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : Video Media Handler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : Sound Media Handler

2.5 跳过第一秒截取视频

ffmpeg -i processed_video01.mp4 -ss 3 cut2_video01.mp4

?查看处理后的视频:

ffmpeg -i cut2_video01.mp4 
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 7 (GCC)
  configuration: 
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cut2_video01.mp4 ':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.45.100
  Duration: 00:06:19.64, start: 0.000000, bitrate: 1179 kb/s
    Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], 1046 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : Video Media Handler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : Sound Media Handler
At least one output file must be specified

文章来源:https://blog.csdn.net/lsb2002/article/details/134965318
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。