Recherche avancée

Médias (91)

Autres articles (89)

  • Qu’est ce qu’un masque de formulaire

    13 juin 2013, par

    Un masque de formulaire consiste en la personnalisation du formulaire de mise en ligne des médias, rubriques, actualités, éditoriaux et liens vers des sites.
    Chaque formulaire de publication d’objet peut donc être personnalisé.
    Pour accéder à la personnalisation des champs de formulaires, il est nécessaire d’aller dans l’administration de votre MediaSPIP puis de sélectionner "Configuration des masques de formulaires".
    Sélectionnez ensuite le formulaire à modifier en cliquant sur sont type d’objet. (...)

  • MediaSPIP v0.2

    21 juin 2013, par

    MediaSPIP 0.2 is the first MediaSPIP stable release.
    Its official release date is June 21, 2013 and is announced here.
    The zip file provided here only contains the sources of MediaSPIP in its standalone version.
    To get a working installation, you must manually install all-software dependencies on the server.
    If you want to use this archive for an installation in "farm mode", you will also need to proceed to other manual (...)

  • Ajouter notes et légendes aux images

    7 février 2011, par

    Pour pouvoir ajouter notes et légendes aux images, la première étape est d’installer le plugin "Légendes".
    Une fois le plugin activé, vous pouvez le configurer dans l’espace de configuration afin de modifier les droits de création / modification et de suppression des notes. Par défaut seuls les administrateurs du site peuvent ajouter des notes aux images.
    Modification lors de l’ajout d’un média
    Lors de l’ajout d’un média de type "image" un nouveau bouton apparait au dessus de la prévisualisation (...)

Sur d’autres sites (8534)

  • How to pipe sequence of thumbnails/screenshots from ffmpeg to nodejs for further processing ?

    30 décembre 2015, par Dmitry Ivanov

    What I want to achieve is to take screenshots continuously and pass them to nodejs application in order to process each one separately and do some other stuff. I need this for Linux environment only. I picked ffmpeg with x11grab as a screenshots provider. The following command works just fine :

    ffmpeg -t 10 -s 1366x768 -f x11grab -i :0.0+0,0 -vf fps=30 output_%d.png -y

    It creates 300 consequent frames of my screen during 10 seconds period. Then I want to redirect the output to my nodejs app rather than just to write files on the hard drive. So I’m calling ffmpeg from node :

    var spawn = require('child_process').spawn,
       fps = 30,
       duration = 10,
       screenSize = {w: 1366, h: 768},
       args = [
           '-t',
           duration,
           '-s',
           screenSize.w + 'x' + screenSize.h,
           '-f',
           'x11grab',
           '-i',
           ':0.0',
           '-vf',
           'fps=' + fps,
           '-f',
           'mjpeg',
           'pipe:1'
       ],
       ff = spawn('ffmpeg', args);

    ff.stdout.on('data', function (data) {
       console.log('Data size: ' + data.length);
    });

    ff.stdout.on('end', function (data) {
       console.log('Stream end');
    });

    ff.stderr.on('data', function (data) {
       console.log('ff error: ' + data);
    });

    I apologize for a long log, but it’s important :

    ff error: ffmpeg version N-77455-g4707497 Copyright (c) 2000-2015 the FFmpeg developers
     built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04)
     configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libdcadec --enable-libfreetype --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvo-aacenc --enable-libvidstab
     libavutil      55. 11.100 / 55. 11.100
     libavcodec     57. 20.100 / 57. 20.100
     libavformat    57. 20.100 / 57. 20.100
     libavdevice    57.  0.100 / 57.  0.100
     libavfilter     6. 21.101 /  6. 21.101
     libavresample   3.  0.  0 /  3.  0.  0
     libswscale      4.  0.100 /  4.  0.100
     libswresample   2.  0.101 /  2.  0.101
     libpostproc    54.  0.100 / 54.  0.100

    ff error: Input #0, x11grab, from ':0.0':
     Duration: N/A, start: 1451414448.216650, bitrate: N/A
       Stream #0:0: Video: rawvideo (BGR[0] / 0x524742), bgr0, 1366x768, 29.97 fps, 29.97 tbr, 1000k tbn,
    ff error: 29.97 tbc

    ff error: [swscaler @ 0x34238a0] deprecated pixel format used, make sure you did set range correctly

    ff error: Output #0, mjpeg, to 'pipe:1':
     Metadata:
       encoder         :
    ff error: Lavf57.20.100
       Stream #0:0: Video: mjpeg, yuvj444p(pc), 1366x768, q=2-31, 200 kb/s, 30 fps, 30 tbn, 30 tbc
       Metadata:
         encoder         : Lavc57.20.100 mjpeg
       Side data:
         unknown side data type 10 (24 bytes)
    Stream mapping:
     Stream #0:0 -> #0:0 (rawvideo (native) -> mjpeg (native))
    Press [q] to stop, [?] for help

    ff error: [swscaler @ 0x34238a0] Warning: data is not aligned! This can lead to a speedloss

    Data size: 65536
    Data size: 65536
    Data size: 21413
    Data size: 65536
    Data size: 65536
    Data size: 45581
    Data size: 65536
    Data size: 65536
    Data size: 62377
    Data size: 65536
    Data size: 65536
    Data size: 45581
    Data size: 65536
    Data size: 65536
    Data size: 21413
    Data size: 65536
    Data size: 60933
    Data size: 65536
    Data size: 49550
    Data size: 65536
    Data size: 36709
    Data size: 65536
    Data size: 27035
    Data size: 65536
    Data size: 20131
    Data size: 65536
    Data size: 15887
    Data size: 65536
    Data size: 15887
    Data size: 65536
    Data size: 15887
    Data size: 65536
    Data size: 15911
    Data size: 65536
    Data size: 15911
    Data size: 65536
    Data size: 15911
    ff error: frame=   16 fps=0.0 q=24.8 size=    1819kB time=00:00:00.53 bitrate=27935.6kbits/s speed=1.04x    
    Data size: 65536
    Data size: 15911
    Data size: 65536
    Data size: 15911
    Data size: 65536
    Data size: 15919
    Data size: 65536
    Data size: 15919
    Data size: 65536
    Data size: 15919
    Data size: 65536
    Data size: 15919
    Data size: 65536
    Data size: 15919
    Data size: 65536
    Data size: 15911
    Data size: 65536
    Data size: 15911
    Data size: 65536
    Data size: 15906
    Data size: 65536
    Data size: 15906
    Data size: 65536
    Data size: 15917
    Data size: 65536
    Data size: 15999
    Data size: 65536
    Data size: 15949
    Data size: 65536
    Data size: 15997
    Data size: 65536
    Data size: 15965
    ff error: frame=   32 fps= 30 q=24.8 size=    3092kB time=00:00:01.06 bitrate=23743.7kbits/s speed=1.01x    
    Data size: 65536
    Data size: 16025
    Data size: 65536
    Data size: 15978
    Data size: 65536
    Data size: 15963
    Data size: 65536
    Data size: 16028
    Data size: 65536
    Data size: 15976
    Data size: 65536
    Data size: 15958
    Data size: 65536
    Data size: 15940
    Data size: 65536
    Data size: 15992
    Data size: 65536
    Data size: 15962
    Data size: 65536
    Data size: 16010
    Data size: 65536
    Data size: 15941
    Data size: 65536
    Data size: 15941
    Data size: 65536
    Data size: 15973
    Data size: 65536
    Data size: 15943
    Data size: 65536
    Data size: 15947
    Data size: 65536
    Data size: 15947
    ff error: frame=   48 fps= 30 q=24.8 size=    4365kB time=00:00:01.60 bitrate=22349.6kbits/s speed=1.01x    
    Data size: 65536
    Data size: 15982
    Data size: 65536
    Data size: 15982
    Data size: 65536
    Data size: 15956
    Data size: 65536
    Data size: 15956
    Data size: 65536
    Data size: 15956
    Data size: 65536
    Data size: 16001
    Data size: 65536
    Data size: 15930
    Data size: 65536
    Data size: 15922
    Data size: 65536
    Data size: 15924
    Data size: 65536
    Data size: 15924
    Data size: 65536
    Data size: 15924
    Data size: 65536
    Data size: 15924
    Data size: 65536
    Data size: 15911
    Data size: 65536
    Data size: 15924
    Data size: 65536
    Data size: 15985
    Data size: 65536
    Data size: 15985
    ff error: frame=   64 fps= 30 q=24.8 size=    5638kB time=00:00:02.13 bitrate=21651.3kbits/s speed=1.01x    
    Data size: 65536
    Data size: 15985
    Data size: 65536
    Data size: 15924
    Data size: 65536
    Data size: 15976
    Data size: 65536
    Data size: 15976
    Data size: 65536
    Data size: 15958
    Data size: 65536
    Data size: 16319
    Data size: 65536
    Data size: 16558
    Data size: 65536
    Data size: 16576
    Data size: 65536
    Data size: 16564
    Data size: 65536
    Data size: 16582
    Data size: 65536
    Data size: 16589
    Data size: 65536
    Data size: 16587
    Data size: 65536
    Data size: 16446
    Data size: 65536
    Data size: 16450
    Data size: 65536
    Data size: 16450
    Data size: 65536
    Data size: 16450
    ff error: frame=   80 fps= 30 q=24.8 size=    6918kB time=00:00:02.66 bitrate=21251.0kbits/s speed=1.01x    
    Data size: 65536
    Data size: 16568
    Data size: 65536
    Data size: 16575
    Data size: 65536
    Data size: 16585
    Data size: 65536
    Data size: 18182
    Data size: 65536
    Data size: 17203
    Data size: 65536
    Data size: 16769
    Data size: 65536
    Data size: 16734
    Data size: 65536
    Data size: 16823
    Data size: 65536
    Data size: 16338
    Data size: 65536
    Data size: 16455
    Data size: 65536
    Data size: 16406
    Data size: 65536
    Data size: 16645
    Data size: 65536
    Data size: 16800
    Data size: 65536
    Data size: 16800
    Data size: 65536
    Data size: 16800
    Data size: 65536
    Data size: 16806
    ff error: frame=   96 fps= 30 q=24.8 size=    8204kB time=00:00:03.20 bitrate=21001.8kbits/s speed=   1x    
    Data size: 65536
    Data size: 16795
    Data size: 65536
    Data size: 16804
    Data size: 65536
    Data size: 16770
    Data size: 65536
    Data size: 16760
    Data size: 65536
    Data size: 16813
    Data size: 65536
    Data size: 16445
    Data size: 65536
    Data size: 16259
    Data size: 65536
    Data size: 16260
    Data size: 65536
    Data size: 16265
    Data size: 65536
    Data size: 16284
    Data size: 65536
    Data size: 16233
    Data size: 65536
    Data size: 16233
    Data size: 65536
    Data size: 16182
    Data size: 65536
    Data size: 16058
    Data size: 60561
    ff error: frame=  111 fps= 30 q=24.8 size=    9384kB time=00:00:03.70 bitrate=20776.1kbits/s speed=   1x    
    Data size: 61813
    Data size: 61813
    Data size: 61813
    Data size: 61813
    Data size: 61813
    Data size: 61781
    Data size: 61784
    Data size: 61796
    Data size: 61842
    Data size: 61839
    Data size: 61793
    Data size: 61810
    Data size: 61844
    Data size: 61844
    Data size: 61850
    Data size: 61841
    ff error: frame=  127 fps= 30 q=24.8 size=   10350kB time=00:00:04.23 bitrate=20027.8kbits/s speed=   1x    
    Data size: 61858
    Data size: 61853
    Data size: 61833
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    ff error: frame=  142 fps= 30 q=24.8 size=   11256kB time=00:00:04.73 bitrate=19480.5kbits/s speed=   1x    
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    ff error: frame=  158 fps= 30 q=24.8 size=   12223kB time=00:00:05.26 bitrate=19011.4kbits/s speed=   1x    
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61867
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    ff error: frame=  173 fps= 30 q=24.8 size=   13129kB time=00:00:05.76 bitrate=18650.3kbits/s speed=   1x    
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    ff error: frame=  188 fps= 30 q=24.8 size=   14035kB time=00:00:06.26 bitrate=18346.6kbits/s speed=   1x    
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    ff error: frame=  203 fps= 30 q=24.8 size=   14941kB time=00:00:06.76 bitrate=18087.8kbits/s speed=   1x    
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    ff error: frame=  219 fps= 30 q=24.8 size=   15907kB time=00:00:07.30 bitrate=17850.8kbits/s speed=   1x    
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    ff error: frame=  234 fps= 30 q=24.8 size=   16813kB time=00:00:07.80 bitrate=17658.1kbits/s speed=   1x    
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    Data size: 61850
    ff error: frame=  250 fps= 30 q=24.8 size=   17780kB time=00:00:08.33 bitrate=17478.0kbits/s speed=   1x    
    Data size: 61850
    Data size: 61712
    Data size: 61712
    Data size: 61712
    Data size: 61712
    Data size: 61712
    Data size: 61712
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    ff error: frame=  265 fps= 30 q=24.8 size=   18675kB time=00:00:08.83 bitrate=17319.4kbits/s speed=   1x    
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    ff error: frame=  280 fps= 30 q=24.8 size=   19563kB time=00:00:09.33 bitrate=17171.2kbits/s speed=   1x    
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    ff error: frame=  295 fps= 30 q=24.8 size=   20452kB time=00:00:09.83 bitrate=17038.0kbits/s speed=   1x    
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    Data size: 60638
    ff error: frame=  300 fps= 30 q=24.8 Lsize=   20748kB time=00:00:10.00 bitrate=16996.6kbits/s speed=0.998x    
    video:20748kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%

    Stream end

    The error messages are weird too, but not the biggest problem for now, so let’s omit them.

    Looking at all the data coming from ffmpeg I assume that the images are coming as a single stream without any separation. I don’t think that looking into binary data and seeking for certain flag bytes is a good idea to solve the problem and separate the images. The command line I mentioned in the top of the post does separate writes to hard drive that means it produces separate writable streams per each screenshot. And it doesn’t do so for a spawned process from nodejs.

    What am I missing ?

  • FFmpeg to get usb camera video and push RSTP stream by c++

    8 octobre 2022, par CrazyJack123

    What I want to do is get usb camera video and push rtsp stream via ffmpeg (not by command).
I've tried a few things and have successfully played RTSP streams through VLC media player using c++.

    


    The problem now is that the rstp video received through the VLC media player has a high delay and is relatively stuck, and it will freeze after a period of time. But this phenomenon does not occur with the ffmpeg command (although there is a little delay, there will be no sucks and freeze).

    


    The ffmpeg command and the c++ code are posted below.Can you help me locate the problem ? any help is greatly appreciated ! Thanks in advance !

    


    By the way, the encoding environment is as follows : windows10, Qt5.9.0 msvc2013_64, ffmpeg-4.4.1-full_build-shared

    


    The ffmpeg command is as follows :

    


    .\ffmpeg.exe -f dshow -rtbufsize 100M -i video="USB Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -rtsp_transport udp -f rtsp rtsp://127.0.0.1/test


    


    c++ code is as follows,here is .h :

    


    #ifndef CAMERATHREADA_H&#xA;#define CAMERATHREADA_H&#xA;&#xA;#include <exception>&#xA;#include <qimage>&#xA;#include <qdebug>&#xA;#include <qcamerainfo>&#xA;#include <qthread>&#xA;#include <qobject>&#xA;using namespace std;&#xA;&#xA;extern "C"&#xA;{&#xA;    #include "libavformat/avformat.h"&#xA;    #include "libavutil/hwcontext.h"&#xA;    #include "libavutil/opt.h"&#xA;    #include "libavutil/time.h"&#xA;    #include "libavutil/frame.h"&#xA;    #include "libavutil/pixdesc.h"&#xA;    #include "libavutil/avassert.h"&#xA;    #include "libavutil/imgutils.h"&#xA;    #include "libavutil/ffversion.h"&#xA;    #include "libavcodec/avcodec.h"&#xA;    #include "libswscale/swscale.h"&#xA;    #include "libavdevice/avdevice.h"&#xA;    #include "libavformat/avformat.h"&#xA;    #include "libavfilter/avfilter.h"&#xA;    #include "libavdevice/avdevice.h"&#xA;    #include "libavcodec/avcodec.h"&#xA;    #include "libavformat/avformat.h"&#xA;    #include "libavutil/pixfmt.h"&#xA;    #include "libswscale/swscale.h"&#xA;    #include "libavutil/time.h"&#xA;    #include "libavutil/mathematics.h"&#xA;}&#xA;&#xA;&#xA;#define FMT_PIC_SHOW AV_PIX_FMT_RGB24&#xA;#define FMT_FRM_PUSH AV_PIX_FMT_YUV420P&#xA;&#xA;&#xA;class CameraThreadA : public QThread&#xA;{&#xA;    Q_OBJECT&#xA;public:&#xA;    CameraThreadA();&#xA;&#xA;signals:&#xA;    void receiveImage(QImage img);&#xA;&#xA;private:&#xA;&#xA;    //code to h264 and push&#xA;    int pushVideoindex;&#xA;    AVCodecContext *pushCodecCtx = nullptr;&#xA;    AVStream *pushStream;&#xA;    AVFormatContext* pushFmtCtx = nullptr;&#xA;    AVPacket* pushPkt = nullptr;&#xA;    AVCodec * pushCodec    = nullptr;&#xA;    uint8_t *pushBuffer;&#xA;    struct SwsContext *swCtxRGB2YUV = nullptr;&#xA;    AVFrame* yuvFrame    = av_frame_alloc();&#xA;&#xA;    //receive from camera&#xA;    AVFormatContext* rcvFmtCtx = nullptr;&#xA;    AVInputFormat*   rcvInFmt  = nullptr;&#xA;    int nVideoIndex          = -1;&#xA;    AVCodecParameters* rcvCodecPara = nullptr;&#xA;    AVCodecContext   * rcvCodecCtx   = nullptr;&#xA;    AVCodec          * rcvCodec    = nullptr;&#xA;    AVFrame* cameraFrame    = av_frame_alloc();&#xA;    AVFrame* rgbFrame = av_frame_alloc();&#xA;    AVPacket* rcvPkt = nullptr;&#xA;    uint8_t* showBuffer;&#xA;    struct SwsContext *rcvSwsCtx = nullptr;&#xA;&#xA;    // QThread interface&#xA;protected:&#xA;    void run();&#xA;};&#xA;&#xA;#endif // CAMERATHREADA_H&#xA;&#xA;&#xA;</qobject></qthread></qcamerainfo></qdebug></qimage></exception>

    &#xA;

    here is .cpp :

    &#xA;

    #include "camerathreada.h"&#xA;&#xA;CameraThreadA::CameraThreadA()&#xA;{&#xA;    //init camera to rgb&#xA;    avdevice_register_all();&#xA;    if(nullptr == (rcvFmtCtx = avformat_alloc_context()))&#xA;    {&#xA;        qDebug() &lt;&lt; "create AVFormatContext failed." &lt;&lt; endl;&#xA;    }&#xA;    if(nullptr == (rcvInFmt = const_cast(av_find_input_format("dshow"))))&#xA;    {&#xA;        qDebug() &lt;&lt; "find AVInputFormat failed." &lt;&lt; endl;&#xA;    }&#xA;    QString urlString = QString("video=USB Camera");&#xA;    if(avformat_open_input(&amp;rcvFmtCtx&#xA;                           , urlString.toStdString().c_str()&#xA;                           , rcvInFmt, NULL) &lt; 0)&#xA;    {&#xA;        qDebug() &lt;&lt; "open camera failed." &lt;&lt; endl;&#xA;    }&#xA;    if(avformat_find_stream_info(rcvFmtCtx, NULL) &lt; 0){&#xA;        qDebug() &lt;&lt; "cannot find stream info." &lt;&lt; endl;&#xA;    }&#xA;    for(size_t i = 0;i &lt; rcvFmtCtx->nb_streams;i&#x2B;&#x2B;){&#xA;        if(rcvFmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){&#xA;            nVideoIndex = i;&#xA;        }&#xA;    }&#xA;    if(nVideoIndex == -1){&#xA;        qDebug() &lt;&lt; "cannot find video stream." &lt;&lt; endl;&#xA;    }&#xA;    rcvCodecPara = rcvFmtCtx->streams[nVideoIndex]->codecpar;&#xA;    if(nullptr == (rcvCodec = const_cast(avcodec_find_decoder(rcvCodecPara->codec_id))))&#xA;    {&#xA;        qDebug() &lt;&lt; "cannot find codec." &lt;&lt; endl;&#xA;    }&#xA;    if(nullptr == (rcvCodecCtx = avcodec_alloc_context3(rcvCodec))){&#xA;        qDebug() &lt;&lt; "cannot alloc codecContext." &lt;&lt; endl;&#xA;    }&#xA;    if(avcodec_parameters_to_context(rcvCodecCtx, rcvCodecPara) &lt; 0){&#xA;        qDebug() &lt;&lt; "cannot initialize codecContext." &lt;&lt; endl;&#xA;    }&#xA;    if(avcodec_open2(rcvCodecCtx, rcvCodec, NULL) &lt; 0){&#xA;        qDebug() &lt;&lt; "cannot open codec." &lt;&lt; endl;&#xA;        return;&#xA;    }&#xA;    rcvSwsCtx = sws_getContext(rcvCodecCtx->width, rcvCodecCtx->height, rcvCodecCtx->pix_fmt,&#xA;                                     rcvCodecCtx->width, rcvCodecCtx->height, FMT_PIC_SHOW,&#xA;                                     SWS_BICUBIC, NULL, NULL, NULL);&#xA;    int numBytes = av_image_get_buffer_size(FMT_PIC_SHOW, rcvCodecCtx->width, rcvCodecCtx->height, 1);&#xA;    showBuffer = (unsigned char*)av_malloc(static_cast<unsigned long="long">(numBytes) * sizeof(unsigned char));&#xA;    if(av_image_fill_arrays(rgbFrame->data, rgbFrame->linesize,&#xA;                            showBuffer&#xA;                            , FMT_PIC_SHOW, rcvCodecCtx->width, rcvCodecCtx->height, 1) &lt; 0)&#xA;    {&#xA;        qDebug() &lt;&lt; "av_image_fill_arrays failed." &lt;&lt; endl;&#xA;    }&#xA;    rcvPkt = av_packet_alloc();&#xA;    av_new_packet(rcvPkt, rcvCodecCtx->width * rcvCodecCtx->height);&#xA;&#xA;&#xA;    //init rgb to yuv&#xA;    swCtxRGB2YUV = sws_getContext(rcvCodecCtx->width, rcvCodecCtx->height, FMT_PIC_SHOW,&#xA;        rcvCodecCtx->width, rcvCodecCtx->height, FMT_FRM_PUSH,&#xA;        SWS_BICUBIC,NULL, NULL, NULL);&#xA;&#xA;    yuvFrame->width = rcvCodecCtx->width;&#xA;    yuvFrame->height = rcvCodecCtx->height;&#xA;    yuvFrame->format = FMT_FRM_PUSH;&#xA;    pushBuffer = (uint8_t *)av_malloc(yuvFrame->width * yuvFrame->height * 1.5);&#xA;    if (av_image_fill_arrays(yuvFrame->data, yuvFrame->linesize&#xA;                             , pushBuffer&#xA;                             , FMT_FRM_PUSH, yuvFrame->width, yuvFrame->height, 1) &lt; 0){&#xA;        qDebug() &lt;&lt; "Failed: av_image_fill_arrays\n";&#xA;    }&#xA;&#xA;&#xA;    //init h264 codec&#xA;    pushCodec = const_cast(avcodec_find_encoder(AV_CODEC_ID_H264));&#xA;    if (!pushCodec){&#xA;        qDebug() &lt;&lt; ("Fail: avcodec_find_encoder\n");&#xA;    }&#xA;    pushCodecCtx = avcodec_alloc_context3(pushCodec);&#xA;    if (!pushCodecCtx){&#xA;        qDebug() &lt;&lt; ("Fail: avcodec_alloc_context3\n");&#xA;    }&#xA;    pushCodecCtx->pix_fmt = FMT_FRM_PUSH;&#xA;    pushCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    pushCodecCtx->width = rcvCodecCtx->width;&#xA;    pushCodecCtx->height = rcvCodecCtx->height;&#xA;    pushCodecCtx->channels = 3;&#xA;    pushCodecCtx->time_base = { 1, 25 };&#xA;    pushCodecCtx->gop_size = 5; &#xA;    pushCodecCtx->max_b_frames = 0;&#xA;    pushCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;    av_opt_set(pushCodecCtx->priv_data, "preset", "ultrafast", 0);&#xA;    av_opt_set(pushCodecCtx->priv_data, "tune", "zerolatency", 0);&#xA;    if (avcodec_open2(pushCodecCtx, pushCodec, NULL) &lt; 0){&#xA;        qDebug() &lt;&lt; ("Fail: avcodec_open2\n");&#xA;    }&#xA;    pushPkt = av_packet_alloc();&#xA;&#xA;&#xA;   //init rtsp pusher&#xA;   QString des = QString("rtsp://127.0.0.1/test")&#xA;   if (avformat_alloc_output_context2(&amp;pushFmtCtx, NULL, "rtsp", des.toStdString().c_str()) &lt; 0){&#xA;       qDebug() &lt;&lt; ("Fail: avformat_alloc_output_context2\n");&#xA;   }&#xA;   av_opt_set(pushFmtCtx->priv_data, "rtsp_transport", "udp", 0);&#xA;   pushFmtCtx->max_interleave_delta = 1000000;&#xA;   pushStream = avformat_new_stream(pushFmtCtx, pushCodec);&#xA;   if (!pushStream){&#xA;       qDebug() &lt;&lt; ("Fail: avformat_new_stream\n");&#xA;   }&#xA;   pushStream->time_base = { 1, 25 };&#xA;   pushVideoindex = pushStream->id = pushFmtCtx->nb_streams - 1;&#xA;   pushCodecCtx->codec_tag = 0;&#xA;   if (pushFmtCtx->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;   {&#xA;     pushCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;   }&#xA;   int ret = 0;&#xA;   ret = avcodec_parameters_from_context(pushStream->codecpar, pushCodecCtx);&#xA;   if (ret &lt; 0)&#xA;   {&#xA;     qDebug() &lt;&lt;("Failed to copy codec context to out_stream codecpar context\n");&#xA;   }&#xA;   //av_dump_format(pushFmtCtx, 0, pushFmtCtx->filename, 1);&#xA;   if (!(pushFmtCtx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;       if (avio_open(&amp;pushFmtCtx->pb, "rtsp://127.0.0.1/test", AVIO_FLAG_WRITE) &lt; 0) {&#xA;           qDebug() &lt;&lt;("Fail: avio_open(&#x27;%s&#x27;)\n rtsp://127.0.0.1/test");&#xA;       }&#xA;   }&#xA;   avformat_write_header(pushFmtCtx, NULL);&#xA;  &#xA;}&#xA;&#xA;void CameraThreadA::run()&#xA;{&#xA;    int testCount = 0;&#xA;    int ret;&#xA;    while(av_read_frame(rcvFmtCtx, rcvPkt) >= 0){&#xA;        if(rcvPkt->stream_index == nVideoIndex){&#xA;            if(avcodec_send_packet(rcvCodecCtx, rcvPkt)>=0){&#xA;                while((ret = avcodec_receive_frame(rcvCodecCtx, cameraFrame)) >= 0){&#xA;                    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;                        return;&#xA;                    else if (ret &lt; 0) {&#xA;                        return;&#xA;                    }&#xA;&#xA;                    //rcv&#xA;                    sws_scale(rcvSwsCtx,&#xA;                              cameraFrame->data, cameraFrame->linesize,&#xA;                              0, rcvCodecCtx->height,&#xA;                              rgbFrame->data, rgbFrame->linesize);&#xA;                    QImage img(showBuffer, rcvCodecCtx->width, rcvCodecCtx->height, QImage::Format_RGB888);&#xA;                    emit receiveImage(img);&#xA;                  &#xA;                    //rgb 2 YUV&#xA;                    if (sws_scale(swCtxRGB2YUV,&#xA;                        rgbFrame->data, rgbFrame->linesize,&#xA;                        0, rcvCodecCtx->height,&#xA;                        yuvFrame->data, yuvFrame->linesize) &lt; 0)&#xA;                    {&#xA;                        qDebug() &lt;&lt; "fail : rgb 2 YUV\n";&#xA;                    }&#xA;                    yuvFrame->pts = av_gettime();&#xA;&#xA;                    //code h264&#xA;                    ret = avcodec_send_frame(pushCodecCtx, yuvFrame);&#xA;                    if (ret &lt; 0){&#xA;                        qDebug() &lt;&lt; "send frame fail\n" &lt;&lt; ret;&#xA;                    }&#xA;                    while (ret >= 0){&#xA;                        ret = avcodec_receive_packet(pushCodecCtx, pushPkt);&#xA;                        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){&#xA;                            qDebug() &lt;&lt;("ret == AVERROR(EAGAIN) || ret == AVERROR_EOF\n");&#xA;                            break;&#xA;                        }else if (ret &lt; 0){&#xA;                            qDebug() &lt;&lt;("Error during encoding\n");&#xA;                            break;&#xA;                        }else{&#xA;                            pushPkt->stream_index = pushVideoindex;&#xA;                            if (av_interleaved_write_frame(pushFmtCtx, pushPkt) &lt; 0) {&#xA;                                qDebug() &lt;&lt; ("Error muxing packet\n");&#xA;                            }&#xA;                            av_packet_unref(pushPkt);&#xA;                        }&#xA;                    }&#xA;                    testCount &#x2B;&#x2B;;&#xA;                    QThread::msleep(10);&#xA;                }&#xA;            }&#xA;            av_packet_unref(rcvPkt);&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;</unsigned>

    &#xA;

  • My python script using ffmpeg captures video content, but the captured content freezes in the middle and jumps frames

    11 novembre 2022, par Supriyo Mitra

    I am new to ffmpeg and I am trying to use it through a python script. The python functions that captures the video content is given below. The problem I am facing is that the captured content freezes at (uneven) intervals and skips a few frames every time it happens.

    &#xA;

    `    def capturelivestream(self, argslist):&#xA;        streamurl, outnum, feedid, outfilename = argslist[0], argslist[1], argslist[2], argslist[3]&#xA;        try:&#xA;            info = ffmpeg.probe(streamurl, select_streams=&#x27;a&#x27;)&#xA;            streams = info.get(&#x27;streams&#x27;, [])&#xA;        except:&#xA;            streams = []&#xA;        if len(streams) == 0:&#xA;            print(&#x27;There are no streams available&#x27;)&#xA;            stream = {}&#xA;        else:&#xA;            stream = streams[0]&#xA;            for stream in streams:&#xA;                if stream.get(&#x27;codec_type&#x27;) != &#x27;audio&#x27;:&#xA;                    continue&#xA;                else:&#xA;                    break&#xA;        if &#x27;channels&#x27; in stream.keys():&#xA;            channels = stream[&#x27;channels&#x27;]&#xA;            samplerate = float(stream[&#x27;sample_rate&#x27;])&#xA;        else:&#xA;            channels = None&#xA;            samplerate = 44100&#xA;        process = ffmpeg.input(streamurl).output(&#x27;pipe:&#x27;, pix_fmt=&#x27;yuv420p&#x27;, format=&#x27;avi&#x27;, vcodec=&#x27;libx264&#x27;, acodec=&#x27;pcm_s16le&#x27;, ac=channels, ar=samplerate, vsync=0, loglevel=&#x27;quiet&#x27;).run_async(pipe_stdout=True)&#xA;        fpath = os.path.dirname(outfilename)&#xA;        fnamefext = os.path.basename(outfilename)&#xA;        fname = fnamefext.split(".")[0]&#xA;        read_size = 320 * 180 * 3 # This is width * height * 3&#xA;        lastcaptured = time.time()&#xA;        maxtries = 12&#xA;        ntries = 0&#xA;        while True:&#xA;            if process:&#xA;                inbytes = process.stdout.read(read_size)&#xA;                if inbytes is not None and inbytes.__len__() > 0:&#xA;                    try:&#xA;                        frame = (np.frombuffer(inbytes, np.uint8).reshape([180, 320, 3]))&#xA;                    except:&#xA;                        print("Failed to reshape frame: %s"%sys.exc_info()[1].__str__())&#xA;                        continue # This could be an issue if there is a continuous supply of frames that cannot be reshaped&#xA;                    self.processq.put([outnum, frame])&#xA;                    lastcaptured = time.time()&#xA;                    ntries = 0&#xA;                else:&#xA;                    if self.DEBUG:&#xA;                        print("Could not read frame for feed ID %s"%feedid)&#xA;                    t = time.time()&#xA;                    if t - lastcaptured > 30: # If the frames can&#x27;t be read for more than 30 seconds...&#xA;                        print("Reopening feed identified by feed ID %s"%feedid)&#xA;                        process = ffmpeg.input(streamurl).output(&#x27;pipe:&#x27;, pix_fmt=&#x27;yuv420p&#x27;, format=&#x27;avi&#x27;, vcodec=&#x27;libx264&#x27;, acodec=&#x27;pcm_s16le&#x27;, ac=channels, ar=samplerate, vsync=0, loglevel=&#x27;quiet&#x27;).run_async(pipe_stdout=True)&#xA;                        ntries &#x2B;= 1&#xA;                    if ntries > maxtries:&#xA;                        if self.DEBUG:&#xA;                            print("Stream %s is no longer available."%streamurl)&#xA;                        # DB statements removed here&#xA;                        &#xA;                        break # Break out of infinite loop.&#xA;                    continue&#xA;        &#xA;        return None`&#xA;&#xA;

    &#xA;

    The function that captures the frames is as follows :

    &#xA;

    &#xA;`    def framewriter(self, outlist):&#xA;        isempty = False&#xA;        endofrun = False&#xA;        while True:&#xA;            frame = None&#xA;            try:&#xA;                args = self.processq.get()&#xA;            except: # Sometimes, the program crashes at this point due to lack of memory...&#xA;                print("Error in framewriter while reading from queue: %s"%sys.exc_info()[1].__str__())&#xA;                continue&#xA;            outnum = args[0]&#xA;            frame = args[1]&#xA;            if outlist.__len__() > outnum:&#xA;                out = outlist[outnum]&#xA;            else:&#xA;                if self.DEBUG == 2:&#xA;                    print("Could not get writer %s"%outnum)&#xA;                continue&#xA;            if frame is not None and out is not None:&#xA;                out.write(frame)&#xA;                isempty = False&#xA;                endofrun = False&#xA;            else:&#xA;                if self.processq.empty() and not isempty:&#xA;                    isempty = True&#xA;                elif self.processq.empty() and isempty: # processq queue is empty now and was empty last time&#xA;                    print("processq is empty")&#xA;                    endofrun = True&#xA;                elif endofrun and isempty:&#xA;                    print("Could not find any frames to process. Quitting")&#xA;                    break&#xA;        print("Done writing feeds. Quitting.")&#xA;        return None`&#xA;

    &#xA;

    The scenario is as follows : There are multiple video streams from a certain website at any time during the day, and the program containing these functions has to capture them as they get streamed. The memory available to this program is 6GB and there could be upto 3 streams running at any instant. Given below is the relevant main section of the script that uses the functions given above.

    &#xA;

    &#xA;

    &#xA;

    `itftennis = VideoBot(siteurl)&#xA;outlist = []&#xA;t = Thread(target=itftennis.framewriter, args=(outlist,))&#xA;t.daemon = True&#xA;t.start()&#xA;tp = Thread(target=handleprocesstermination, args=())&#xA;tp.daemon = True&#xA;tp.start()&#xA;# Create a database connection and as associated cursor object. We will handle database operations from main thread only.&#xA;# DB statements removed from here...&#xA;feedidlist = []&#xA;vidsdict = {}&#xA;streampattern = re.compile("\?vid=(\d&#x2B;)$")&#xA;while True:&#xA;    streampageurls = itftennis.checkforlivestream()&#xA;    if itftennis.DEBUG:&#xA;        print("Checking for new urls...")&#xA;        print(streampageurls.__len__())&#xA;    if streampageurls.__len__() > 0:&#xA;        argslist = []&#xA;        newurlscount = 0&#xA;        for streampageurl in streampageurls:&#xA;            newstream = False&#xA;            sps = re.search(streampattern, streampageurl)&#xA;            if sps:&#xA;                streamnum = sps.groups()[0]&#xA;                if streamnum not in vidsdict.keys(): # Check if this stream has already been processed.&#xA;                    vidsdict[streamnum] = 1&#xA;                    newstream = True&#xA;                else:&#xA;                    continue&#xA;            else:&#xA;                continue&#xA;            print("Detected new live stream... Getting it.")&#xA;            streamurl = itftennis.getstreamurlfrompage(streampageurl)&#xA;            print("Adding %s to list..."%streamurl)&#xA;            if streamurl is not None:&#xA;                # Now, get feed metadata...&#xA;                metadata = itftennis.getfeedmetadata(streampageurl)&#xA;                if metadata is None:&#xA;                    continue&#xA;                # lines to get matchescounter omitted here...&#xA;                if matchescounter >= itftennis.__class__.MAX_CONCURRENT_MATCHES:&#xA;                    break&#xA;                if newstream is True:&#xA;                    newurlscount &#x2B;= 1&#xA;                outfilename = time.strftime("./videodump/" &#x2B; "%Y%m%d%H%M%S",time.localtime())&#x2B;".avi"&#xA;                out = open(outfilename, "wb")&#xA;                outlist.append(out) # Save it in the list and take down the number for usage in framewriter&#xA;                outnum = outlist.__len__() - 1&#xA;                # Save metadata in DB&#xA;                # lines omitted here....&#xA;                argslist.append([streamurl, outnum, feedid, outfilename])   &#xA;            else:&#xA;                print("Couldn&#x27;t get the stream url from page")&#xA;        if newurlscount > 0:&#xA;            for args in argslist:&#xA;                try:&#xA;                    p = Process(target=itftennis.capturelivestream, args=(args,))&#xA;                    p.start()&#xA;                    processeslist.append(p)&#xA;                    if itftennis.DEBUG:&#xA;                        print("Started process with args %s"%args)&#xA;                except:&#xA;                    print("Could not start process due to error: %s"%sys.exc_info()[1].__str__())&#xA;            print("Created processes, continuing now...")&#xA;            continue&#xA;    time.sleep(itftennis.livestreamcheckinterval)&#xA;t.join()&#xA;tp.join()&#xA;for out in outlist:&#xA;    out.close()`&#xA;

    &#xA;

    &#xA;

    &#xA;

    Please accept my apologies for swamping with this amount of code. I wanted to provide maximum context to my problem. I have removed the absolutely irrelevant DB statements, but apart from that this is what the code looks like.

    &#xA;

    If you need to know anything else about the code, please let me know. What I would really like to know is if I am using the ffmpeg streams capturing statements correctly. The stream contains both video and audio components and I need to capture both. Hence I am making the following call :

    &#xA;

    process = ffmpeg.input(streamurl).output(&#x27;pipe:&#x27;, pix_fmt=&#x27;yuv420p&#x27;, format=&#x27;avi&#x27;, vcodec=&#x27;libx264&#x27;, acodec=&#x27;pcm_s16le&#x27;, ac=channels, ar=samplerate, vsync=0, loglevel=&#x27;quiet&#x27;).run_async(pipe_stdout=True)&#xA;

    &#xA;

    Is this how it is supposed to be done ? More importantly, why do I keep getting the freezes in the output video. I have monitored the streams manually, and they are quite consistent. Frame losses do not happen when I view them on the website (at least it is not obviously noticeable). Also, I have run 'top' command on the host running the program. The CPU usage sometimes go over 100% (which, I came to understand from some answers on SO, is to be expected when running ffmpeg) but the memory usage usually remain below 30%. So what is the issue here. What do I need to do in order to fix this problem (other than learn more about how ffmpeg works).

    &#xA;

    Thanks

    &#xA;

    I have tried using various ffmpeg options (while trying to find similar issues that others encountered). I also tried running ffmpeg from command line for a limited period of time (11 mins), using the same options as used in the python code, and the captured content came out quite well. No freezes. No jumps in frames. But I need to use it in an automated way and there would be multiple streams at any time. Also, when I try playing the captured content using ffplay, I sometimes get the message "co located POCs unavailable" when these freezes happen. What does it mean ?

    &#xA;