我编写了一个模拟摄像头的程序,并将输出转换为视频流.该程序必须能够在 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;
}

在Windows上使用FFMPEG从图像流式传输视频的更多相关文章

  1. HTML5 播放 RTSP 视频的实例代码

    目前大多数网络摄像头都是通过 RTSP 协议传输视频流的,但是 HTML 并不标准支持 RTSP 流。本文重点给大家介绍HTML5 播放 RTSP 视频的实例代码,需要的朋友参考下吧

  2. Html5 滚动穿透的方法

    这篇文章主要介绍了Html5 滚动穿透的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  3. HTML5 拖放(Drag 和 Drop)详解与实例代码

    本篇文章主要介绍了HTML5 拖放(Drag 和 Drop)详解与实例代码,具有一定的参考价值,有兴趣的可以了解一下

  4. 跨域修改iframe页面内容详解

    这篇文章主要介绍了跨域修改iframe页面内容详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 暂停调度队列是否会暂停其目标队列?

    我想创建两个串行队列A&B.队列B是队列A的目标.我想在B上排队一些块,并暂停它直到我准备执行它们,但是我想继续在队列A上执行块.如果我暂停B,这还会暂停它的目标队列(队列A)吗?我的想法是,我想安排这些特定的块在稍后日期执行但是我不希望它们同时运行而我不这样做想要处理信号量.但我希望队列A继续处理它的块,而B则被暂停如果不清楚这里是一些示例代码解决方法queueB被挂起,但queueA未被挂起.queueA和queueB被挂起.

  7. ios – 使用CocoaPods post install hook将自定义路径添加到HEADER_SEARCH_PATHS

    解决方法在Podfile中定义一个方法:然后在post_install中调用该方法:

  8. 如何使用多对等连接将摄像头从一个iOS设备流式传输到另一个设备

    我们如何在iOS7中使用蓝牙或wifi有效地将摄像头源从一个iOS设备传输到另一个iOS设备.下面是获取流缓冲区的代码.在这里,我们可以获得iOS相机捕获的图像.我们是否可以使用多个对等方将样本缓冲区信息直接发送到另一个设备,或者是否有任何有效的方法将数据传输到其他iOS设备?

  9. 在下载iOS时流式传输视频

    有人知道可能导致这个问题吗?我知道一些文件系统支持“文件大小”和“磁盘大小”作为两个不同的属性…不知道如果iOS有这样的东西?

  10. ios – 在Swift中删除WKWebView Accesory栏

    我现在正试着将this转换成Swift而没有真正的背景.这是我到目前为止所得到的…而且我一直在寻找谷歌并不知道要搜索什么才能更具体.你能否详细说明我做错了什么?

随机推荐

  1. static – 在页面之间共享数据的最佳实践

    我想知道在UWP的页面之间发送像’selectedItem’等变量的最佳做法是什么?创建一个每个页面都知道的静态全局变量类是一个好主意吗?

  2. .net – 为Windows窗体控件提供百分比宽度/高度

    WindowsForm开发的新手,但在Web开发方面经验丰富.有没有办法为Windows窗体控件指定百分比宽度/高度,以便在用户调整窗口大小时扩展/缩小?当窗口调整大小时,可以编写代码来改变控件的宽度/高度,但我希望有更好的方法,比如在HTML/CSS中.在那儿?

  3. 使用Windows Azure查询表存储数据

    我需要使用特定帐户吗?>将应用程序部署到Azure服务后,如何查询数据?GoogleAppEngine有一个数据查看器/查询工具,Azure有类似的东西吗?>您可以看到的sqlExpressintance仅在开发结构中,并且一旦您表示没有等效,所以请小心使用它.>您可以尝试使用Linqpad查询表格.看看JamieThomson的thispost.

  4. windows – SetupDiGetClassDevs是否与文档中的设备实例ID一起使用?

    有没有更好的方法可以使用DBT_DEVICEARRIVAL事件中的数据获取设备的更多信息?您似乎必须指定DIGCF_ALLCLASSES标志以查找与给定设备实例ID匹配的所有类,或者指定ClassGuid并使用DIGCF_DEFAULT标志.这对我有用:带输出:

  5. Windows Live ID是OpenID提供商吗?

    不,WindowsLiveID不是OpenID提供商.他们使用专有协议.自从他们的“测试版”期结束以来,他们从未宣布计划继续它.

  6. 如果我在代码中进行了更改,是否需要重新安装Windows服务?

    我写了一个Windows服务并安装它.现在我对代码进行了一些更改并重新构建了解决方案.我还应该重新安装服务吗?不,只需停止它,替换文件,然后重新启动它.

  7. 带有双引号的字符串回显使用Windows批处理输出文件

    我正在尝试使用Windows批处理文件重写配置文件.我循环遍历文件的行并查找我想要用指定的新行替换的行.我有一个’函数’将行写入文件问题是%Text%是一个嵌入双引号的字符串.然后失败了.可能还有其他角色也会导致失败.如何才能使用配置文件中的所有文本?尝试将所有“在文本中替换为^”.^是转义字符,因此“将被视为常规字符你可以尝试以下方法:其他可能导致错误的字符是:

  8. .net – 将控制台应用程序转换为服务?

    我正在寻找不同的优势/劣势,将我们长期使用的控制台应用程序转换为Windows服务.我们为ActiveMQ使用了一个叫做java服务包装器的东西,我相信人们告诉我你可以用它包装任何东西.这并不是说你应该用它包装任何东西;我们遇到了这个问题.控制台应用程序是一个.NET控制台应用程序,默认情况下会将大量信息记录到控制台,尽管这是可配置的.任何推荐?我们应该在VisualStudio中将其重建为服务吗?我使用“-install”/“-uninstall”开关执行此操作.例如,seehere.

  9. windows – 捕获外部程序的STDOUT和STDERR *同时*它正在执行(Ruby)

    哦,我在Windows上:-(实际上,它比我想象的要简单,这看起来很完美:…是的,它适用于Windows!

  10. windows – 当我试图批量打印变量时,为什么我得到“Echo is on”

    我想要执行一个简单的批处理文件脚本:当我在XP中运行时,它给了我预期的输出,但是当我在Vista或Windows7中运行它时,我在尝试打印值时得到“EchoisOn”.以下是程序的输出:摆脱集合表达式中的空格.等号(=)的两侧可以并且应该没有空格BTW:我通常在@echo关闭的情况下启动所有批处理文件,并以@echo结束它们,所以我可以避免将代码与批处理文件的输出混合.它只是使您的批处理文件输出更好,更清洁.

返回
顶部