
Recherche avancée
Médias (91)
-
MediaSPIP Simple : futur thème graphique par défaut ?
26 septembre 2013, par
Mis à jour : Octobre 2013
Langue : français
Type : Video
-
avec chosen
13 septembre 2013, par
Mis à jour : Septembre 2013
Langue : français
Type : Image
-
sans chosen
13 septembre 2013, par
Mis à jour : Septembre 2013
Langue : français
Type : Image
-
config chosen
13 septembre 2013, par
Mis à jour : Septembre 2013
Langue : français
Type : Image
-
SPIP - plugins - embed code - Exemple
2 septembre 2013, par
Mis à jour : Septembre 2013
Langue : français
Type : Image
-
GetID3 - Bloc informations de fichiers
9 avril 2013, par
Mis à jour : Mai 2013
Langue : français
Type : Image
Autres articles (89)
-
Qu’est ce qu’un masque de formulaire
13 juin 2013, parUn 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, parMediaSPIP 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, parPour 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 IvanovWhat 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
withx11grab
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 endThe 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 CrazyJack123What 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
#define CAMERATHREADA_H

#include <exception>
#include <qimage>
#include <qdebug>
#include <qcamerainfo>
#include <qthread>
#include <qobject>
using namespace std;

extern "C"
{
 #include "libavformat/avformat.h"
 #include "libavutil/hwcontext.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/frame.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/ffversion.h"
 #include "libavcodec/avcodec.h"
 #include "libswscale/swscale.h"
 #include "libavdevice/avdevice.h"
 #include "libavformat/avformat.h"
 #include "libavfilter/avfilter.h"
 #include "libavdevice/avdevice.h"
 #include "libavcodec/avcodec.h"
 #include "libavformat/avformat.h"
 #include "libavutil/pixfmt.h"
 #include "libswscale/swscale.h"
 #include "libavutil/time.h"
 #include "libavutil/mathematics.h"
}


#define FMT_PIC_SHOW AV_PIX_FMT_RGB24
#define FMT_FRM_PUSH AV_PIX_FMT_YUV420P


class CameraThreadA : public QThread
{
 Q_OBJECT
public:
 CameraThreadA();

signals:
 void receiveImage(QImage img);

private:

 //code to h264 and push
 int pushVideoindex;
 AVCodecContext *pushCodecCtx = nullptr;
 AVStream *pushStream;
 AVFormatContext* pushFmtCtx = nullptr;
 AVPacket* pushPkt = nullptr;
 AVCodec * pushCodec = nullptr;
 uint8_t *pushBuffer;
 struct SwsContext *swCtxRGB2YUV = nullptr;
 AVFrame* yuvFrame = av_frame_alloc();

 //receive from camera
 AVFormatContext* rcvFmtCtx = nullptr;
 AVInputFormat* rcvInFmt = nullptr;
 int nVideoIndex = -1;
 AVCodecParameters* rcvCodecPara = nullptr;
 AVCodecContext * rcvCodecCtx = nullptr;
 AVCodec * rcvCodec = nullptr;
 AVFrame* cameraFrame = av_frame_alloc();
 AVFrame* rgbFrame = av_frame_alloc();
 AVPacket* rcvPkt = nullptr;
 uint8_t* showBuffer;
 struct SwsContext *rcvSwsCtx = nullptr;

 // QThread interface
protected:
 void run();
};

#endif // CAMERATHREADA_H


</qobject></qthread></qcamerainfo></qdebug></qimage></exception>


here is
.cpp
:

#include "camerathreada.h"

CameraThreadA::CameraThreadA()
{
 //init camera to rgb
 avdevice_register_all();
 if(nullptr == (rcvFmtCtx = avformat_alloc_context()))
 {
 qDebug() << "create AVFormatContext failed." << endl;
 }
 if(nullptr == (rcvInFmt = const_cast(av_find_input_format("dshow"))))
 {
 qDebug() << "find AVInputFormat failed." << endl;
 }
 QString urlString = QString("video=USB Camera");
 if(avformat_open_input(&rcvFmtCtx
 , urlString.toStdString().c_str()
 , rcvInFmt, NULL) < 0)
 {
 qDebug() << "open camera failed." << endl;
 }
 if(avformat_find_stream_info(rcvFmtCtx, NULL) < 0){
 qDebug() << "cannot find stream info." << endl;
 }
 for(size_t i = 0;i < rcvFmtCtx->nb_streams;i++){
 if(rcvFmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){
 nVideoIndex = i;
 }
 }
 if(nVideoIndex == -1){
 qDebug() << "cannot find video stream." << endl;
 }
 rcvCodecPara = rcvFmtCtx->streams[nVideoIndex]->codecpar;
 if(nullptr == (rcvCodec = const_cast(avcodec_find_decoder(rcvCodecPara->codec_id))))
 {
 qDebug() << "cannot find codec." << endl;
 }
 if(nullptr == (rcvCodecCtx = avcodec_alloc_context3(rcvCodec))){
 qDebug() << "cannot alloc codecContext." << endl;
 }
 if(avcodec_parameters_to_context(rcvCodecCtx, rcvCodecPara) < 0){
 qDebug() << "cannot initialize codecContext." << endl;
 }
 if(avcodec_open2(rcvCodecCtx, rcvCodec, NULL) < 0){
 qDebug() << "cannot open codec." << endl;
 return;
 }
 rcvSwsCtx = sws_getContext(rcvCodecCtx->width, rcvCodecCtx->height, rcvCodecCtx->pix_fmt,
 rcvCodecCtx->width, rcvCodecCtx->height, FMT_PIC_SHOW,
 SWS_BICUBIC, NULL, NULL, NULL);
 int numBytes = av_image_get_buffer_size(FMT_PIC_SHOW, rcvCodecCtx->width, rcvCodecCtx->height, 1);
 showBuffer = (unsigned char*)av_malloc(static_cast<unsigned long="long">(numBytes) * sizeof(unsigned char));
 if(av_image_fill_arrays(rgbFrame->data, rgbFrame->linesize,
 showBuffer
 , FMT_PIC_SHOW, rcvCodecCtx->width, rcvCodecCtx->height, 1) < 0)
 {
 qDebug() << "av_image_fill_arrays failed." << endl;
 }
 rcvPkt = av_packet_alloc();
 av_new_packet(rcvPkt, rcvCodecCtx->width * rcvCodecCtx->height);


 //init rgb to yuv
 swCtxRGB2YUV = sws_getContext(rcvCodecCtx->width, rcvCodecCtx->height, FMT_PIC_SHOW,
 rcvCodecCtx->width, rcvCodecCtx->height, FMT_FRM_PUSH,
 SWS_BICUBIC,NULL, NULL, NULL);

 yuvFrame->width = rcvCodecCtx->width;
 yuvFrame->height = rcvCodecCtx->height;
 yuvFrame->format = FMT_FRM_PUSH;
 pushBuffer = (uint8_t *)av_malloc(yuvFrame->width * yuvFrame->height * 1.5);
 if (av_image_fill_arrays(yuvFrame->data, yuvFrame->linesize
 , pushBuffer
 , FMT_FRM_PUSH, yuvFrame->width, yuvFrame->height, 1) < 0){
 qDebug() << "Failed: av_image_fill_arrays\n";
 }


 //init h264 codec
 pushCodec = const_cast(avcodec_find_encoder(AV_CODEC_ID_H264));
 if (!pushCodec){
 qDebug() << ("Fail: avcodec_find_encoder\n");
 }
 pushCodecCtx = avcodec_alloc_context3(pushCodec);
 if (!pushCodecCtx){
 qDebug() << ("Fail: avcodec_alloc_context3\n");
 }
 pushCodecCtx->pix_fmt = FMT_FRM_PUSH;
 pushCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
 pushCodecCtx->width = rcvCodecCtx->width;
 pushCodecCtx->height = rcvCodecCtx->height;
 pushCodecCtx->channels = 3;
 pushCodecCtx->time_base = { 1, 25 };
 pushCodecCtx->gop_size = 5; 
 pushCodecCtx->max_b_frames = 0;
 pushCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 av_opt_set(pushCodecCtx->priv_data, "preset", "ultrafast", 0);
 av_opt_set(pushCodecCtx->priv_data, "tune", "zerolatency", 0);
 if (avcodec_open2(pushCodecCtx, pushCodec, NULL) < 0){
 qDebug() << ("Fail: avcodec_open2\n");
 }
 pushPkt = av_packet_alloc();


 //init rtsp pusher
 QString des = QString("rtsp://127.0.0.1/test")
 if (avformat_alloc_output_context2(&pushFmtCtx, NULL, "rtsp", des.toStdString().c_str()) < 0){
 qDebug() << ("Fail: avformat_alloc_output_context2\n");
 }
 av_opt_set(pushFmtCtx->priv_data, "rtsp_transport", "udp", 0);
 pushFmtCtx->max_interleave_delta = 1000000;
 pushStream = avformat_new_stream(pushFmtCtx, pushCodec);
 if (!pushStream){
 qDebug() << ("Fail: avformat_new_stream\n");
 }
 pushStream->time_base = { 1, 25 };
 pushVideoindex = pushStream->id = pushFmtCtx->nb_streams - 1;
 pushCodecCtx->codec_tag = 0;
 if (pushFmtCtx->oformat->flags & AVFMT_GLOBALHEADER)
 {
 pushCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 }
 int ret = 0;
 ret = avcodec_parameters_from_context(pushStream->codecpar, pushCodecCtx);
 if (ret < 0)
 {
 qDebug() <<("Failed to copy codec context to out_stream codecpar context\n");
 }
 //av_dump_format(pushFmtCtx, 0, pushFmtCtx->filename, 1);
 if (!(pushFmtCtx->oformat->flags & AVFMT_NOFILE)) {
 if (avio_open(&pushFmtCtx->pb, "rtsp://127.0.0.1/test", AVIO_FLAG_WRITE) < 0) {
 qDebug() <<("Fail: avio_open('%s')\n rtsp://127.0.0.1/test");
 }
 }
 avformat_write_header(pushFmtCtx, NULL);
 
}

void CameraThreadA::run()
{
 int testCount = 0;
 int ret;
 while(av_read_frame(rcvFmtCtx, rcvPkt) >= 0){
 if(rcvPkt->stream_index == nVideoIndex){
 if(avcodec_send_packet(rcvCodecCtx, rcvPkt)>=0){
 while((ret = avcodec_receive_frame(rcvCodecCtx, cameraFrame)) >= 0){
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
 return;
 else if (ret < 0) {
 return;
 }

 //rcv
 sws_scale(rcvSwsCtx,
 cameraFrame->data, cameraFrame->linesize,
 0, rcvCodecCtx->height,
 rgbFrame->data, rgbFrame->linesize);
 QImage img(showBuffer, rcvCodecCtx->width, rcvCodecCtx->height, QImage::Format_RGB888);
 emit receiveImage(img);
 
 //rgb 2 YUV
 if (sws_scale(swCtxRGB2YUV,
 rgbFrame->data, rgbFrame->linesize,
 0, rcvCodecCtx->height,
 yuvFrame->data, yuvFrame->linesize) < 0)
 {
 qDebug() << "fail : rgb 2 YUV\n";
 }
 yuvFrame->pts = av_gettime();

 //code h264
 ret = avcodec_send_frame(pushCodecCtx, yuvFrame);
 if (ret < 0){
 qDebug() << "send frame fail\n" << ret;
 }
 while (ret >= 0){
 ret = avcodec_receive_packet(pushCodecCtx, pushPkt);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){
 qDebug() <<("ret == AVERROR(EAGAIN) || ret == AVERROR_EOF\n");
 break;
 }else if (ret < 0){
 qDebug() <<("Error during encoding\n");
 break;
 }else{
 pushPkt->stream_index = pushVideoindex;
 if (av_interleaved_write_frame(pushFmtCtx, pushPkt) < 0) {
 qDebug() << ("Error muxing packet\n");
 }
 av_packet_unref(pushPkt);
 }
 }
 testCount ++;
 QThread::msleep(10);
 }
 }
 av_packet_unref(rcvPkt);
 }
 }
}

</unsigned>


-
My python script using ffmpeg captures video content, but the captured content freezes in the middle and jumps frames
11 novembre 2022, par Supriyo MitraI 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.


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




The function that captures the frames is as follows :



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



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.






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







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.


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 :


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



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).


Thanks


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 ?