类似指令:
ffmpeg -f avfoundation -s 1920x1080 -framerate 25 -i 0: -vsync 0 -f image2 /path/2/.../wframe/%06d.jpg
直接运行:
ffmpeg version N-92539-g1035206102 Copyright (c) 2000-2018 the FFmpeg developers
built with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
...
[avfoundation @ 0x7fa94c800000] Selected pixel format (yuv420p) is not supported by the input device.
[avfoundation @ 0x7fa94c800000] Supported pixel formats:
[avfoundation @ 0x7fa94c800000] uyvy422
[avfoundation @ 0x7fa94c800000] yuyv422
[avfoundation @ 0x7fa94c800000] nv12
[avfoundation @ 0x7fa94c800000] 0rgb
[avfoundation @ 0x7fa94c800000] bgr0
[avfoundation @ 0x7fa94c800000] Overriding selected pixel format to use uyvy422 instead.
[avfoundation @ 0x7fa94c800000] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, avfoundation, from '0:':
Duration: N/A, start: 11907.278033, bitrate: N/A
Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 1920x1080, 1000k tbr, 1000k tbn, 1000k tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0x7fa94b871600] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to '/path/2/.../wframe/%06d.jpg':
Metadata:
encoder : Lavf58.23.100
Stream #0:0: Video: mjpeg, yuvj422p(pc), 1920x1080, q=2-31, 200 kb/s, 1000k fps, 1000k tbn, 1000k tbc
Metadata:
encoder : Lavc58.40.100 mjpeg
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame= 55 fps= 16 q=24.8 Lsize=N/A time=00:00:03.56 bitrate=N/A speed=1.01x
...
没有问题...
核心代码:
import subprocess, time
cmd = ['ffmpeg'
, '-f', 'avfoundation'
, '-s', '1920x1080'
, '-framerate', '25' # frames per second
, '-i', '{}:'.format(drivers['w'])
, '-vsync', '0'
, '-f', 'image2'
, '{}/wframe/%06d.jpg'.format(_expath)
]
p = subprocess.Popen(cmd
, shell=False
, stdin=subprocess.PIPE
#, stdout=subprocess.PIPE
# merge err>out
#, stderr=subprocess.STDOUT
)
time.sleep(4)
p.terminate()
用 subprocess.Popen
包裹后, 在合理时机用 terminate()
来安全结束,
可是在 mac 中运行时报错:
[avfoundation @ 0x7f9f01000000] Failed to create AV capture input device: Cannot Use UNIQUESKY_CAR_CAMERA
0:: Input/output error
修订 shell=True
,或是打开其它 PIPE 都不能解决;
相同硬件, 到 win10 环境中, 用相同版本 FFmpeg 来调用, 调整好的指令类似:
ffmpeg -f dshow -s:v 1920x1080 -framerate 25 video="Integrated Webcam" -f image2 /path/2/.../wframe/%06d.jpg
一样直接在 cmd 中运行正常, 但是, 用 python 包装后, 一样出现不可用报错:
: Input/output error
[avfoundation @ 0x7fa5f7802c00] Failed to create AV capture input device: Cannot Use UNIQUESKY_CAR_CAMERA #2
0:: Input/output error
: 设备争用
[dshow @ 0000014269bab740] Could not run graph (sometimes caused by a device already in use by other application)
video=@device_pnp_\\?\usb#vid_05a3&pid_9230&mi_00#6&1d3a4c24&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global: I/O error
此时检查:
λ taskkill /IM ffmpeg.exe
错误: 没有找到进程 "ffmpeg.exe"。
并没有其它进程在使用硬件
用 FFmpeg 在 win10 环境中检验设备时是这样的
λ ffmpeg -list_devices true -f dshow -i dummy
ffmpeg version N-92539-g1035206102 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 8.2.1 (GCC) 20181017
...
[dshow @ 0000021f90a2a840] DirectShow video devices (some may be both video and audio devices)
[dshow @ 0000021f90a2a840] "Integrated Webcam"
[dshow @ 0000021f90a2a840] Alternative name "@device_pnp_\\?\usb#vid_1bcf&pid_0b09&mi_00#7&1bf8ceab&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
[dshow @ 0000021f90a2a840] DirectShow audio devices
[dshow @ 0000021f90a2a840] "楹﹀厠椋?(4- USB PnP Sound Device)"
[dshow @ 0000021f90a2a840] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{25AB1848-CB77-42E5-9985-767755AA0C9C}"
dummy: Immediate exit requested
即, 所有可用设备有两种指代:
有老司机称:
其实是这样的,windows 那个 cmd,如果你给它双引号,好像也是会被 shell 吃掉的,然后程序的 argv 没有引号,跟 bash 有点类似;
另外,
但是, 按照以上建议无论怎么折腾都是无法正常通过 Python 完成 FFMpeg 的调用.
大家有什么其它思路?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.