在Windows上使用FFMPEG从图像流式传输视频

我编写了一个模拟摄像头的程序,并将输出转换为视频流.该程序必须能够在 Windows上运行.
系统中有两个组件:

>相机模拟器.一个模拟相机的C程序.它使用windows copy命令每0.1秒将预先生成的帧(即PNG文件)复制到目标路径./target/target_image.png
>视频流.使用FFmpeg,它可以从复制的图像中创建视频流.使用以下命令运行FFmpeg:
ffmpeg -loop 1 -i ./target/target_image.png -r 10 -vcodec mpeg4 -f mpegts udp://127.0.0.1:1234

当整个运行一起运行时,它可以正常运行几秒钟,直到ffmpeg停止运行.这是在调试模式下运行时的日志:

ffmpeg version N-52458-gaa96439 Copyright (c) 2000-2013 the FFmpeg developers
  built on Apr 24 2013 22:19:32 with gcc 4.8.0 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 27.101 / 52. 27.101
  libavcodec     55.  6.100 / 55.  6.100
  libavformat    55.  3.100 / 55.  3.100
  libavdevice    55.  0.100 / 55.  0.100
  libavfilter     3. 60.101 /  3. 60.101
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  3.100 / 52.  3.100
Splitting the commandline.
Reading option '-loop' ... matched as AVOption 'loop' with argument '1'.
Reading option '-i' ... matched as input file with argument './target/target_image.png'.
Reading option '-r' ... matched as option 'r' (set frame rate (Hz value,fraction or abbreviation)) with argument '10'.
Reading option '-vcodec' ... matched as option 'vcodec' (force video codec ('copy' to copy stream)) with argument 'mpeg4'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'mpegts'.
Reading option 'udp://127.0.0.1:1234' ... matched as output file.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option loglevel (set logging level) with argument debug.
Successfully parsed a group of options.
Parsing a group of options: input file ./target/target_image.png.
Successfully parsed a group of options.
Opening an input file: ./target/target_image.png.
[AVIOContext @ 02678840] Statistics: 234307 bytes read,0 seeks
[AVIOContext @ 02678840] Statistics: 221345 bytes read,0 seeks
    Last message repeated 1 times
[AVIOContext @ 02678840] Statistics: 226329 bytes read,0 seeks
    Last message repeated 2 times
[AVIOContext @ 02678840] Statistics: 228676 bytes read,0 seeks
    Last message repeated 2 times
[AVIOContext @ 02678840] Statistics: 230685 bytes read,0 seeks
    Last message repeated 2 times
[AVIOContext @ 02678840] Statistics: 232697 bytes read,0 seeks
    Last message repeated 5 times
[AVIOContext @ 02678840] Statistics: 234900 bytes read,0 seeks
    Last message repeated 2 times
[AVIOContext @ 02678840] Statistics: 236847 bytes read,0 seeks
[image2 @ 02677ac0] Probe buffer size limit of 5000000 bytes reached
Input #0,image2,from './target/target_image.png':
  Duration: 00:00:00.04,start: 0.000000,bitrate: N/A
    Stream #0:0,22,1/25: Video: png,rgb24,1274x772 [SAR 1:1 DAR 637:386],1/25,25 fps,25 tbr,25 tbn,25 tbc
Successfully opened the file.
Parsing a group of options: output file udp://127.0.0.1:1234.
Applying option r (set frame rate (Hz value,fraction or abbreviation)) with argument 10.
Applying option vcodec (force video codec ('copy' to copy stream)) with argument mpeg4.
Applying option f (force format) with argument mpegts.
Successfully parsed a group of options.
Opening an output file: udp://127.0.0.1:1234.
Successfully opened the file.
[graph 0 input from stream 0:0 @ 02769280] Setting 'video_size' to value '1274x772'
[graph 0 input from stream 0:0 @ 02769280] Setting 'pix_fmt' to value '2'
[graph 0 input from stream 0:0 @ 02769280] Setting 'time_base' to value '1/25'
[graph 0 input from stream 0:0 @ 02769280] Setting 'pixel_aspect' to value '1/1'
[graph 0 input from stream 0:0 @ 02769280] Setting 'sws_param' to value 'flags=2'
[graph 0 input from stream 0:0 @ 02769280] Setting 'frame_rate' to value '25/1'
[graph 0 input from stream 0:0 @ 02769280] w:1274 h:772 pixfmt:rgb24 tb:1/25 fr:25/1 sar:1/1 sws_param:flags=2
[format @ 02768ba0] compat: called with args=[yuv420p]
[format @ 02768ba0] Setting 'pix_fmts' to value 'yuv420p'
[auto-inserted scaler 0 @ 02768740] Setting 'w' to value '0'
[auto-inserted scaler 0 @ 02768740] Setting 'h' to value '0'
[auto-inserted scaler 0 @ 02768740] Setting 'flags' to value '0x4'
[auto-inserted scaler 0 @ 02768740] w:0 h:0 flags:'0x4' interl:0
[format @ 02768ba0] auto-inserting filter 'auto-inserted scaler 0' between the filter 'Parsed_null_0' and the filter 'format'
[AVFilterGraph @ 026772c0] query_formats: 4 queried,3 merged,1 already done,0 delayed
[auto-inserted scaler 0 @ 02768740] w:1274 h:772 fmt:rgb24 sar:1/1 -> w:1274 h:772 fmt:yuv420p sar:1/1 flags:0x4
[mpeg4 @ 02785020] detected 4 logical cores
[mpeg4 @ 02785020] intra_quant_bias = 0 inter_quant_bias = -64
[mpegts @ 0277da40] muxrate VBR,pcr every 1 pkts,sdt every 200,pat/pmt every 40 pkts
Output #0,mpegts,to 'udp://127.0.0.1:1234':
  Metadata:
    encoder         : Lavf55.3.100
    Stream #0:0,1/90000: Video: mpeg4,yuv420p,1/10,q=2-31,200 kb/s,90k tbn,10 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (png -> mpeg4)
Press [q] to stop,[?] for help
*** drop!
    Last message repeated 10 times
frame=   11 fps=0.0 q=4.0 size=     118kB time=00:00:01.10 bitrate= 875.1kbits/s dup=0 drop=11    
Statistics: 242771 bytes read,0 seeks
[AVIOContext @ 02674a60] Statistics: 246525 bytes read,0 seeks
*** drop!
[AVIOContext @ 02674a60] Statistics: 230678 bytes read,0 seeks
[AVIOContext @ 02674a60] Statistics: 244023 bytes read,0 seeks
*** drop!
[AVIOContext @ 02674a60] Statistics: 246389 bytes read,0 seeks

*** drop!
[AVIOContext @ 02674a60] Statistics: 224478 bytes read,0 seeks
[AVIOContext @ 02674a60] Statistics: 228013 bytes read,0 seeks
*** drop!
[image2 @ 02677ac0] Could not open file : ./target/target_image.png
./target/target_image.png: Input/output error
[output stream 0:0 @ 02768c20] EOF on sink link output stream 0:0:default.
No more output streams to write to,finishing.
frame=  164 fps= 17 q=31.0 Lsize=     959kB time=00:00:16.40 bitrate= 478.9kbits/s dup=0 drop=240    

video:869kB audio:0kB subtitle:0 global headers:0kB muxing overhead 10.285235%
404 frames successfully decoded,0 decoding errors
[AVIOContext @ 026779c0] Statistics: 0 seeks,746 writeouts

在我看来,读/写同一文件之间存在某种冲突.有趣的是,在Linux上(用cp替换副本),程序运行得很好.

有人可以提出解决这个问题的方法吗?只要逻辑工作流程保持不变,替代解决方案也是可接受的.

在Nick van Tilborg的评论之后,我最终使用了FFmpeg的image2pipe.此功能允许将图像数据简化为FFmpeg,而不是同时访问同一文件的两个进程.

下面是我写的C代码.它可能没有完全优化,但它完成了工作.它是在Windows 7上使用Visual Studio 2012编译和测试的.

#include "stdafx.h"
#include "windows.h"
#include "iostream"

#include "stdio.h"

using namespace std;

#pragma warning (disable : 4996)
int _tmain(int argc,_TCHAR* argv[])
{
    int count;
    int times = 2200;
    FILE *pPipe;
    FILE * pFile;
    long lSize;
    char * buffer;
    size_t result;

    // open a pipe to FFmpeg
    if( (pPipe = _popen( "ffmpeg -re -f image2pipe -vcodec mjpeg -i - -vcodec h264 -r 10 -f mpegts udp://127.0.0.1:1234","wb")) == NULL ) {exit( 1 );}

    for ( count = 1; count <= times; count++) {
        char filename[40];
        sprintf(&filename[0],".\\images\\image-%07d.jpg",count);

        pFile = fopen ( filename,"rb" );
        if (pFile==NULL) {fputs ("File error",stderr); exit (2);}

        // obtain file size:
        fseek (pFile,SEEK_END);
        lSize = ftell (pFile);
        rewind (pFile);

        // allocate memory to contain the whole file:
        buffer = (char*) malloc (sizeof(char)*lSize);
        if (buffer == NULL) {fputs ("Memory error",stderr); exit (3);}

        // copy the file into the buffer:
        result = fread (buffer,1,lSize,pFile);
        if (result != lSize) {fputs ("Reading error",stderr); exit (4);}

        // write to pipe
        fwrite(buffer,pPipe); 
        fflush(pPipe);

        // clean
        fclose (pFile);
        free (buffer);

        //
        Sleep(100);
    }

    //
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


文章浏览阅读2.2k次,点赞6次,收藏20次。在我们平时办公工作中,很多时候我们经常会使用到虚拟机来进行环境的测试,我们平时在虚拟机上接触的最多的莫过于Linux和Winwdos。不过虚拟机环境和物理机环境是无法直接传输的,那么有的时候呢,同学们又想要在两者之间相互传输文件,可能就会使用QQ邮箱等形式来传输,这样的效率又慢而且繁琐,今天我就为大家带来一种非常便捷的传输方式。通过XFTP工具来进行文件传输。_xftp连接windows
文章浏览阅读1k次。解决 Windows make command not found 和 安装 GCC 环境_windows下载gcc
文章浏览阅读3.2k次,点赞2次,收藏6次。2、鼠标依次点击“计算机配置“ - ”管理模板“ - ”网络“ - ”Lanman工作站”,点击右侧的“启用不安全的来宾登录”策略。Windows访问samba共享时,提示“你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问”1、键盘按下window+R键,输入gpedit.msc,启动本地组策略编辑器。首先在终端中输入sudo ufw status查看当前防火墙状态。默认状态是“未配置”,修改为“已启用”。示例:创建一个narada的目录在/home下。1.更新apt储存库列表。_ubuntu samba 目标文件夹访问被拒绝
文章浏览阅读1.3w次。蓝光版属于高清版的一种。BD英文全名是Blu-ray Disc,一种高清的电影版本,这种电影十分清晰但是数据量巨大,占数十G甚至上百G的容量,只有蓝光光碟才能装得下,所以这种高清电影被称为BD版。一般的高清电影多半是从蓝光电影、国外的高清电视频道上压制而来的,可以通过网络下载,多数都经过二次压缩,画质要逊于原视频,不过压缩后的容量从蓝光的25G-50G会减少成4G-8G等(15G-20G不等)。众所周知,视频有两种常见的清晰度,BD和HD,在看电影的时候最常出现这两个标志,那么BD和HD具体指的是什么呢?_bd hd
文章浏览阅读974次,点赞7次,收藏8次。提供了更强大的功能,因为它允许直接访问当前元素,而不需要类型转换。接口,它可以提供一个迭代器,用于按顺序访问集合中的元素。接口是只读的,它只能支持前向迭代,不能修改集合中的元素。类型的集合实例,并向其中添加了几个元素。接口,可以创建一个能够迭代访问泛型集合中元素的迭代器。接口,我们可以在 C# 中实现可迭代的集合,并使用。循环和迭代器手动遍历集合,并输出每个元素的值。接口表示一个可枚举的集合,它定义了一个方法。属性,用于获取集合中当前位置的元素。存储集合中的元素,并实现了。的泛型集合类,它实现了。
文章浏览阅读1.4w次,点赞5次,收藏22次。如果使用iterator的remove方法则会正常,因为iterator的remove方法会在内部调用List的remove方法,但是会修改excepedModCount的值,因此会正常运行。因为遍历过程中进行remove 操作时,该位置后面的元素会挤到前面来,这时候会发生一种情况就是原来元素的位置会被他后面的元素取代,而该位置已经遍历过了,所以该元素不会背遍历。当我们倒序遍历元素的时候,无论删除元素之后的元素怎么移动,之前的元素对应的索引(index)是不会发生变化的,所以在删除元素的时候不会发生问题。_list删除某个元素
文章浏览阅读2.9w次,点赞45次,收藏192次。Windows下配置Visual Studio _vs2022环境变量配置
文章浏览阅读7w次,点赞162次,收藏778次。pip 是Python包管理工具,提供了对 Python 包的查找、下载、安装、卸载的功能,目前Python 3.4 和 2.7 及以上版本都有配套安装,一般pip的位置在...pythonScripts文件夹里面,而在其他版本需要自行下载。_python pip install安装
文章浏览阅读5.8k次,点赞2次,收藏12次。①此电脑右击----->选择属性----->高级系统设置----->环境变量----->path----->编辑----->新建。第一个选项意思就是将安装路径填入到系统环境变量中,这里勾选,后面使用可能会出现问题,建议不要勾选,安装好之后手动添加环境变量。注意:如果提示conda不是内部或外部命令,原因是Anaconda的环境变量没配置好。如果不想立即打开anaconda,不勾选直接finish就好。②输入 conda --version ,查看conda环境。②直接按win键,搜索“环境变量”_windows安装anaconda
文章浏览阅读5.1k次,点赞8次,收藏55次。Windows 系统从零配置 Python 环境,安装CUDA、CUDNN、PyTorch 详细教程_windows cuda cudnn配置
文章浏览阅读1.5w次,点赞54次,收藏68次。macOS系统自带有VNC远程桌面,我们可以在控制端上安装配置VNC客户端,以此来实现远程控制macOS。但通常需要在不同网络下进行远程控制,为此,我们可以在macOS被控端上使用cpolar做内网穿透,映射VNC默认端口5900,通过所生成的公网地址,来实现在公网环境下远程控制VNC。_vnc mac
文章浏览阅读2.4k次,点赞5次,收藏11次。进入后根据自己的电脑系统下载,这是python 3.10版本下载地址,如果想要下载其它版本可进入此链接(下载完成后点击进行安装点击下一步,到这一步时,可以选择将Anaconda添加我的PATH环境变量中,这样就不用自己手动配置和环境变量。安装完成后,打开终端,输出 python 命令可查看是否安装成功。如果显示自己刚才安装的版本号说明安装成功。查看conda版本命令:conda info。_paddlespeech下载
文章浏览阅读3.3k次。所以如果要删除之前新增的课程编译原理,只需输入命令del Course:8:Cname,同时还应该把本课程的学分删除del Course:8:Ccredit,如下图所示;Redis并没有修改数据的命令,所以如果在Redis中要修改一条数据,只能在使用set命令时,使用同样的键值,然后用新的value值来覆盖旧的数据。先调用get命令,输出原先的值,然后set新的值,最后再get得到新值,所以修改成功。输入命令后没有报错,表示成功了,刷新windows的服务,多了一个redis服务。_redis windows服务
文章浏览阅读2.1w次,点赞9次,收藏56次。​​接着在【工作负荷】中,选择【使用C++桌面开发】 ,右边【安装详细信息】去除其它可选项,只勾选【MSVCv142 】和 【Windows 10 SDK】,按图示修改,然后右下角点击安装,之后会有提示让你重启电脑。重启电脑之后,再进行pip安装。报错原因是pip所安装的包需要使用C++编译后才能够正常安装,但是当前安装环境中缺少完整的C++编译环境,因此安装失败。3.安装Microsoft Visual C++ Build Tool离线安装包(1个多G),CSDN资源很多,需要积分下载,_error: microsoft visual c++ 14.0 or greater is required. get it with "micros
文章浏览阅读1.1w次,点赞3次,收藏7次。Step 3: 在右侧窗口中找到名称为“LongPathsEnabled”的“DWORD (32 位) 值”条目,并双击它。通过注册表方法或组策略方法启用长路径支持后,您将能够在 Windows 中使用长路径,并能够访问和处理长路径下的文件和文件夹。Step 2: 依次选择“计算机配置” > “管理模板” > “系统” > “文件资源管理器”。Step 3: 找到“启用 Win32 长路径”设置,双击它。Step 4: 选择“已启用”选项按钮,然后选择“应用”按钮。_windows长路径支持
文章浏览阅读2.5k次,点赞81次,收藏86次。
文章浏览阅读1.3k次,点赞65次,收藏50次。顺序表,链表,栈,队列,ArrayList,LinkedList,Stack,Queue
文章浏览阅读2.3k次,点赞2次,收藏2次。AnyTXTSearcher是一款能够帮助我们对文档以及文本内容进行快速搜索和管理的工具,通过该软件能够搜索各种Office文档,文本文件,代码,PDF文档等,顶级的全文搜索引擎1秒钟之内即可完成搜索。_anytxt searcher
文章浏览阅读8.8k次,点赞73次,收藏70次。有时,在删除/移动/重命名文件夹/文件时,会遇到如下警告,即使将打开的程序关闭了,后台也可能会有没关干净的相关进程。_解除占用
文章浏览阅读4.3w次,点赞91次,收藏102次。JDK(Java Development Kit)是Java开发工具包的缩写,包含了Java编译器、Java虚拟机、Java类库等众多组件,是Java开发的基石,提供了编写、编译和运行Java程序所必需的工具。同时,为了让系统能够正确识别Java环境,在开始使用JDK进行Java开发之前,需要先把JDK安装到本地计算机,并配置好相应的环境变量。本文将介绍JDK安装与环境变量配置的方法。_windows安装jdk并配置环境变量