找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 59|回复: 0

FFmpeg提取视频中的音频

[复制链接]

2万

主题

140

回帖

14万

积分

管理员

积分
140180
发表于 2023-7-3 15:31:34 | 显示全部楼层 |阅读模式 IP:山东省临沂市 电信

登录后更精彩...O(∩_∩)O...

您需要 登录 才可以下载或查看,没有账号?立即注册

×
FFmpeg提取视频(mp4)中的音频(m4a)目的方法

官方:https://trac.ffmpeg.org/wiki/Encode/MP3
最有效的:https://stackoverflow.com/questions/9913032/how-can-i-extract-audio-from-video-with-ffmpeg

1. 下载 FFmpeg
下载地址 :https://github.com/BtbN/FFmpeg-Builds/releases
下载后解压缩,我解压缩到的位置是:D:\Program Files
并将文件夹名称改为ffmpeg
自己可以根据情况,随便解压缩到什么位置都可以


2. 设置windows环境变量

将 ffmpeg.exe 所在的目录添加到windows环境变量中,用户变量或系统变量都可以。我这里是D:\Program Files\ffmpeg\bin
8.png


3. 命令行模式下,测试是否可以正常运行

CMD打开命令行模式,在任意文件夹位置下运行ffmpeg,如果出现如下提示,即可。

[Bash shell] 纯文本查看 复制代码
D:\>ffmpeg
ffmpeg version N-100461-gb0a882cc93 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  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 --enable-shared --disable-static --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-opencl --enable-libvmaf --enable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-libdavs2 --enable-ffnvcodec --enable-cuda-llvm --enable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-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-libs=-lgomp
  libavutil      56. 62.100 / 56. 62.100
  libavcodec     58.115.102 / 58.115.102
  libavformat    58. 65.100 / 58. 65.100
  libavdevice    58. 11.103 / 58. 11.103
  libavfilter     7. 94.100 /  7. 94.100
  libswscale      5.  8.100 /  5.  8.100
  libswresample   3.  8.100 /  3.  8.100
  libpostproc    55.  8.100 / 55.  8.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

D:\>


4. 开工-FFmpeg提取视频(mp4)中的音频(m4a)

我的E盘根目录下有个mp4文件 e:\He is sorry里_s_到底发几次?.mp4
要提取其中的音频,并存为 e:\He is sorry里_s_到底发几次?.m4a
输入以下命令

[Bash shell] 纯文本查看 复制代码
D:\>ffmpeg -i "e:\He is sorry里_s_到底发几次?".mp4 -vn -codec copy "e:\He is sorry里_s_到底发几次?".m4a

[Bash shell] 纯文本查看 复制代码
D:\>ffmpeg -i "e:\He is sorry里_s_到底发几次?".mp4 -vn -codec copy "e:\He is sorry里_s_到底发几次?".m4a
ffmpeg version N-100461-gb0a882cc93 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  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 --enable-shared --disable-static --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-opencl --enable-libvmaf --enable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-libdavs2 --enable-ffnvcodec --enable-cuda-llvm --enable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-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-libs=-lgomp
  libavutil      56. 62.100 / 56. 62.100
  libavcodec     58.115.102 / 58.115.102
  libavformat    58. 65.100 / 58. 65.100
  libavdevice    58. 11.103 / 58. 11.103
  libavfilter     7. 94.100 /  7. 94.100
  libswscale      5.  8.100 /  5.  8.100
  libswresample   3.  8.100 /  3.  8.100
  libpostproc    55.  8.100 / 55.  8.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'e:\He is sorry里_s_到底发几次?.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.12.100
  Duration: 00:05:44.96, start: 0.000000, bitrate: 1333 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 1199 kb/s, 24 fps, 24 tbr, 12288 tbn, 48 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Output #0, ipod, to 'e:\He is sorry里_s_到底发几次?.m4a':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.65.100
    Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:1 -> #0:0 (copy)
Press [q] to stop, [?] for help
size=    5473kB time=00:05:44.93 bitrate= 130.0kbits/s speed=5.95e+03x
video:0kB audio:5414kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.087956%

D:\>


[Bash shell] 纯文本查看 复制代码
ffmpeg -i Sample.avi -vn -ar 44100 -ac 2 -ab 192k -f mp3 Sample.mp3


5. 进阶-添加到右键菜单,实现一键提取
复制以下代码,保存为文件名:提取音频mp3.reg

[Bash shell] 纯文本查看 复制代码
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\ffmpeg]
@="提取音频mp4->mp3"

[HKEY_CLASSES_ROOT\*\shell\ffmpeg\command]
@="\"d:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe\" -i %1 -q:a 0 -map a %1.mp3"


复制以下代码,保存为文件名:提取音频m4a.reg


[Bash shell] 纯文本查看 复制代码
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\ffmpeg]
@="提取音频mp4->m4a"

[HKEY_CLASSES_ROOT\*\shell\ffmpeg\command]
@="\"d:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe\" -i %1 -vn -codec copy %1.m4a"


不同的写法:

[Bash shell] 纯文本查看 复制代码
ffmpeg -i .\TMElive.mp4 -ss 01:00:31 -t 00:02:52.0 -vn -b:a 192k -ar 44100 -ac 2 -acodec libmp3lame -y .\Mute_1.mp3


[Bash shell] 纯文本查看 复制代码
root@jackfan: ~$ ffmpeg -i test.mp4 -f mp3 -vn output-audio.mp3

参数解释:

-i 表示input,即输入文件

-f 表示format,即输出格式

-vn表示vedio not,即输出不包含视频

acodec copy 说使用已经在那里相同的音频流。

c:a 代表codec audio的意思

ss 选项指定开始时间戳

t 选项指定编码持续时间

-qscale:a 2 : The 2 means 190 kb/sec
Seems like you're extracting audio from a video file & downmixing to stereo channel.
To just extract audio (without re-encoding):
[Bash shell] 纯文本查看 复制代码
ffmpeg -i in.mp4 -vn -c:a copy out.m4a
To extract audio & [color=var(--theme-link-color)]downmix to stereo (without re-encoding):
[Bash shell] 纯文本查看 复制代码
ffmpeg -i in.mp4 -vn -c:a copy -ac 2 out.m4a
To generate an mp3 file, you'd re-encode audio:
[Bash shell] 纯文本查看 复制代码
ffmpeg -i in.mp4 -vn -ac 2 out.mp3
-c (select codecs) & -map (select streams) options:
-c:a -> select best supported audio (transcoded)
-c:a copy -> best supported audio (copied)
-map 0:a -> all audio from 1st (audio) input file (transcoded)
-map 0:0 -> 1st stream from 1st input file (transcoded)
-map 1:a:0 -> 1st audio stream from 2nd (audio) input file (transcoded)
-map 1:a:1 -c:a copy -> 2nd audio stream from 2nd (audio)input file (copied)


主要选项


‘-f fmt (input/output)’

        强制输入或输出文件格式。通常,输入文件的格式是自动检测的,

        输出文件的格式是通过文件扩展名来进行猜测的,所有该选项大

        多数时候不需要。


‘-i filename (input)’

        输入文件名


‘-y (global)’

        覆盖输出文件而不询问


‘-n (global)’

        不覆盖输出文件,如果一个给定的输出文件已经存在,则立即

        退出


‘-t duration (output)’

        当到达 duration 时,停止写输出。

        duration 可以是一个数字(秒),或者使用hh:mm:ss[.xxx]形式。

        -to 和 -t 是互斥的,-t 优先级更高。


‘-to position (output)’

        在 position 处停止写输出。

        duration 可以是一个数字(秒),或者使用hh:mm:ss[.xxx]形式。

        -to 和 -t 是互斥的,-t 优先级更高。


‘-ss position (input/output)’

        当作为输入选项时(在 -i 之前),在输入文件中跳转到 position。

        需要注意的是,在大多数格式中,不太可能精确的跳转,因此,

        ffmpeg 将跳转到 position 之前最接近的位置。当进行转码

        并且 ‘-accurate_seek’ 打开时(默认),位于跳转点和 position

        之间的额外部分将被解码并且丢弃。当做流拷贝或者当使用

        ‘-noaccurate_seek’时,它将被保留下来。

        当作为输出选项时(在输出文件名前),解码但是丢弃输入,直到

        时间戳到达 position。


        position 可以是秒或者 hh:mm:ss[.xxx] 形式



音频选项


‘-ar[:stream_specifier] freq (input/output,per-stream)’

        设置音频采样率。


‘-aq q (output)’

        设置音频质量。这是 -q:a 的别名


‘-ac[:stream_specifier] channels (input/output,per-stream)’

        设置音频通道数。


‘-vn (output)’

        禁止视频录制


‘-an (output)’

        禁止音频录制


‘-acodec codec (input/output)’

        设置音频codec。这是-codec:a的别名


‘-sample_fmt[:stream_specifier] sample_fmt (output,per-stream)’


        设置音频采样格式



高级选项
‘-map [-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]] | [linklabel] (output)’
        指定一个或多个流作为输出文件的源。
        命令行中的第一个 -map 选项,指定输出流0的源,
        第二个 -map 选项,指定输出流1的源,等等。









回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|时间戳|加密|CTF WiKi|CTF平台汇总|CTF show|ctfhub|棱角安全|rutracker|攻防世界|php手册|peiqi文库|CyberChef|猫捉鱼铃|手机版|小黑屋|cn-sec|IOTsec-Zone|在线工具|分享屋 ( 鲁ICP备2021028754号 )

GMT+8, 2024-9-22 15:29

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表