笔记本电脑下载python视频-如何在Windows中使用ffmpeg抓取笔记本电脑摄像头视频...

1586010002-jmsa.png

I have a small python program that works very well to capture short videos from webcams in linux (at least for laptops that have built-in webcams) using a sub-process with ffmpeg.

Now i'm trying to write the same program to capture webcams in windows, and i know i can't use the generic "/dev/video0" that works pretty well in linux, but i thought something like naming it "Integrated Camera" should be enough, but it fails.

Here's my linux code (that works):

import sys

from subprocess import call

from datetime import datetime

def record_webcam(seconds):

cam = '/dev/video0'

timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')

filename = timestamp + 'something.mkv' #generated with more complexity in the actual code, but that isn't important

ffmpeg_cmd = 'ffmpeg -t {} -an -i {} -c:v libx264 -preset veryslow -crf 25 {}'.format(seconds, cam, filename).split()

p = call(ffmpeg_cmd)

return filename if p == 0 else False

if __name__ == '__main__':

record_webcam(sys.argv[1])

I have looked at the documentation for ffmpeg and tried to search for solution but so far i'm lost...

I know that "Integrated Camera"s are only available on some laptops and not others, and that it won't capture other cameras connected, but it's enough for my use case... but if you want a challenge I would also like to know how to apply it to any windows-pc with a camera regardless of what it's called.

Also, is it easier or more recommended to do what i'm trying here only with python tools, like OpenCV?

Thanks in advance!

Edit: I answered my own question with a partial solution if anyone is interested based on a comment from @Mulvya, but if anyone can still explain to me the part about OpenCV I would still like to hear it...

解决方案

I did it... based on what @Mulvya commented, i was able to list all cameras withing the code and extract a command from them, i now have a different issue about that, but i will ask another question for it. In the meanwhile, if anyone is interested in automatically selecting the first available camera on windows through python and ffmpeg, my solution is this:

import re

from subprocess import Popen, PIPE

list_cmd = 'ffmpeg -list_devices true -f dshow -i dummy'.split()

p = Popen(list_cmd, stderr=PIPE)

for line in iter(p.stderr.readline,''):

if flagcam:

cam = re.search('".*"',line.decode(encoding='UTF-8')).group(0)

cam = 'video=' + cam if cam else ''

flagcam = False

elif 'DirectShow video devices'.encode(encoding='UTF-8') in line:

flagcam = True

elif 'Immediate exit requested'.encode(encoding='UTF-8') in line:

break

the variable "cam" now holds the name of the cam as it is in DirectShow on windows

Follow up question here if anyone wants to help EDIT: also solved

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用C语言和FFmpeg打开笔记本电脑摄像头,首先需要在Windows系统下安装好FFmpeg库。安装完成后,可以使用以下代码来实现: 1. 首先,包含FFmpeg的头文件和其他必要的库文件。 ```c #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <windows.h> #include <libavcodec/avcodec.h> #include <libavdevice/avdevice.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> ``` 2. 初始化FFmpeg并打开摄像头。 ```c int main() { // 初始化FFmpeg av_register_all(); avformat_network_init(); avdevice_register_all(); AVFormatContext* formatContext = NULL; // 打开摄像头 AVInputFormat* inputFormat = av_find_input_format("dshow"); avformat_open_input(&formatContext, "video=Integrated Webcam", inputFormat, NULL); avformat_find_stream_info(formatContext, NULL); // 查找并打开视频流 int videoStream = -1; for (int i = 0; i < formatContext->nb_streams; i++) { if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { printf("无法打开视频流。\n"); return -1; } // 读取视频帧 AVPacket packet; av_init_packet(&packet); AVCodecContext* codecContext = formatContext->streams[videoStream]->codec; AVCodec* codec = avcodec_find_decoder(codecContext->codec_id); avcodec_open2(codecContext, codec, NULL); AVFrame* frame = av_frame_alloc(); AVFrame* frameRGB = av_frame_alloc(); int numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, codecContext->width, codecContext->height); uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t)); avpicture_fill((AVPicture*)frameRGB, buffer, AV_PIX_FMT_RGB24, codecContext->width, codecContext->height); struct SwsContext* swsContext = sws_getContext( codecContext->width, codecContext->height, codecContext->pix_fmt, codecContext->width, codecContext->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL ); while (av_read_frame(formatContext, &packet) >= 0) { if (packet.stream_index == videoStream) { avcodec_decode_video2(codecContext, frame, &frameFinished, &packet); if (frameFinished) { sws_scale( swsContext, frame->data, frame->linesize, 0, codecContext->height, frameRGB->data, frameRGB->linesize ); // 在这里可以对图像帧进行处理 } } av_packet_unref(&packet); } // 清理资源 av_frame_free(&frame); av_frame_free(&frameRGB); avcodec_close(codecContext); avformat_close_input(&formatContext); avformat_network_deinit(); return 0; } ``` 这个代码片段会打开笔记本电脑摄像头读取摄像头返回的图像帧,并将其存储在RGB格式的帧。你可以根据需要,在代码加入对图像帧的处理逻辑。最后,记得清理资源并关闭摄像头。 注意:这只是一个简单的示例,实际应用可能需要处理更多的异常情况和错误处理。另外,由于某些Windows系统使用摄像头驱动不兼容FFmpeg,可能需要额外的配置和处理才能正常工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值