视频转音频
- 安装依赖
pip install ffmpy3
- 报错1
点击查看详情
目录已存在,即将保存!
ffmpeg -i C:\work\brucepk\test\7251538642030611746.mp4 -vn -ar 44100 -ac 2 -ab 192 -f wav C:\work\brucepk\output\7251538642030611746.wav
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\ffmpy3.py", line 120, in run
stderr=stderr
File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\work\eclipse-workspace\demo\src\test1\__init__.py", line 37, in <module>
ff.run()
File "C:\ProgramData\Anaconda3\lib\site-packages\ffmpy3.py", line 124, in run
raise FFExecutableNotFoundError("Executable '{0}' not found".format(self.executable))
ffmpy3.FFExecutableNotFoundError: Executable 'ffmpeg' not found
-
解决方案,安装ffmpeg
-
直接解压,复制路径
-
配置环境变量
-
打开cmd测试
-
新建文件夹,将mp4视频放到如下目录
-
完整代码
import os
from ffmpy3 import FFmpeg
def mkdir_output(output_dir):
existence = os.path.exists(output_dir)
if not existence:
print('创建音频存放目录')
os.makedirs(output_dir) # 创建目录
os.chdir(output_dir) # 切换到创建的文件夹
return True
else:
print('目录已存在,即将保存!')
return False
if __name__ == '__main__':
filepath = r"C:\work\brucepk\test" # 待转换视频存放的路径
os.chdir(filepath) # 切换到改路径下
filename = os.listdir(filepath) # 得到文件夹下的所有文件名称
output_dir = r'C:\work\brucepk\output' # 转换后音频文件存放的路径
mkdir_output(output_dir)
for i in range(len(filename)):
# windows电脑记得把下面两处的 "/" 换成 "\\"
changefile = filepath+"\\"+filename[i]
outputfile = output_dir+"\\"+filename[i].replace('mp4', 'wav').replace('mkv', 'wav')\
.replace('rmvb', 'wav').replace('3gp', 'wav').replace('avi', 'wav')\
.replace('mpeg', 'wav').replace('mpg', 'wav').replace('dat', 'asf')\
.replace('wmv', 'wav').replace('flv', 'wav').replace('mov', 'wav')\
.replace('mp4', 'wav').replace('ogg', 'wav').replace('ogm', 'wav')\
.replace('rm', 'wav')
ff = FFmpeg(
executable='C:\\work\\ffmpeg\\bin\\ffmpeg.exe',
inputs={changefile: None},
outputs={outputfile: '-vn -ar 44100 -ac 2 -ab 192 -f wav'}
)
print(ff.cmd)
ff.run()
- 控制台输出
点击查看详情
目录已存在,即将保存!
C:\work\ffmpeg\bin\ffmpeg.exe -i C:\work\brucepk\test\7251538642030611746.mp4 -vn -ar 44100 -ac 2 -ab 192 -f wav C:\work\brucepk\output\7251538642030611746.wav
ffmpeg version 6.1.1-essentials_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 --pkg-config=pkgconf --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --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-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
libavutil 58. 29.100 / 58. 29.100
libavcodec 60. 31.102 / 60. 31.102
libavformat 60. 16.100 / 60. 16.100
libavdevice 60. 3.100 / 60. 3.100
libavfilter 9. 12.100 / 9. 12.100
libswscale 7. 5.100 / 7. 5.100
libswresample 4. 12.100 / 4. 12.100
libpostproc 57. 3.100 / 57. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\work\brucepk\test\7251538642030611746.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
comment : vid:v0300fg10000ciha66rc77u1l7n4i2i0
Duration: 00:00:37.82, start: 0.033008, bitrate: 4157 kb/s
Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1080x1920 [SAR 1:1 DAR 9:16], 3706 kb/s, 60 fps, 60 tbr, 15360 tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 64 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Stream mapping:
Stream #0:1 -> #0:0 (aac (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
[pcm_s16le @ 000001ea28c0bc00] Bitrate 192 is extremely low, maybe you mean 192k
Output #0, wav, to 'C:\work\brucepk\output\7251538642030611746.wav':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
ICMT : vid:v0300fg10000ciha66rc77u1l7n4i2i0
ISFT : Lavf60.16.100
Stream #0:0(und): Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
encoder : Lavc60.31.102 pcm_s16le
size= 0kB time=00:00:00.06 bitrate= 0.0kbits/s speed=N/A
[out#0/wav @ 000001ea2919e540] video:0kB audio:6504kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.001862%
size= 6504kB time=00:00:37.79 bitrate=1409.6kbits/s speed= 738x
- 文件夹输出,使用播放器播放,音频与视频符合