
Recherche avancée
Médias (1)
-
Rennes Emotion Map 2010-11
19 octobre 2011, par
Mis à jour : Juillet 2013
Langue : français
Type : Texte
Autres articles (76)
-
Le profil des utilisateurs
12 avril 2011, parChaque utilisateur dispose d’une page de profil lui permettant de modifier ses informations personnelle. Dans le menu de haut de page par défaut, un élément de menu est automatiquement créé à l’initialisation de MediaSPIP, visible uniquement si le visiteur est identifié sur le site.
L’utilisateur a accès à la modification de profil depuis sa page auteur, un lien dans la navigation "Modifier votre profil" est (...) -
Configurer la prise en compte des langues
15 novembre 2010, parAccéder à la configuration et ajouter des langues prises en compte
Afin de configurer la prise en compte de nouvelles langues, il est nécessaire de se rendre dans la partie "Administrer" du site.
De là, dans le menu de navigation, vous pouvez accéder à une partie "Gestion des langues" permettant d’activer la prise en compte de nouvelles langues.
Chaque nouvelle langue ajoutée reste désactivable tant qu’aucun objet n’est créé dans cette langue. Dans ce cas, elle devient grisée dans la configuration et (...) -
XMP PHP
13 mai 2011, parDixit Wikipedia, XMP signifie :
Extensible Metadata Platform ou XMP est un format de métadonnées basé sur XML utilisé dans les applications PDF, de photographie et de graphisme. Il a été lancé par Adobe Systems en avril 2001 en étant intégré à la version 5.0 d’Adobe Acrobat.
Étant basé sur XML, il gère un ensemble de tags dynamiques pour l’utilisation dans le cadre du Web sémantique.
XMP permet d’enregistrer sous forme d’un document XML des informations relatives à un fichier : titre, auteur, historique (...)
Sur d’autres sites (9092)
-
FFmpeg RTP example error
20 juillet 2016, par JohnnylinCan anyone show me an example code of how to use FFmpeg to encode a ".mp4" video and then output to network stream using RTP(rtp ://127.0.0.1:6666). I have searched for it on google but most of them are just command line. Thanks very much.
UPDATED :
extern "C"{
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libswscale></libswscale>swscale.h>
#include <libavutil></libavutil>avutil.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>time.h>
}
#include
#include <iostream>
#include <string>
using namespace std;
// compatibility with newer API
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc avcodec_alloc_frame
#define av_frame_free avcodec_free_frame
#endif
#define RNDTO2(X) ( ( (X) & 0xFFFFFFFE ))
#define RNDTO32(X) ( ( (X) % 32 ) ? ( ( (X) + 32 ) & 0xFFFFFFE0 ) : (X) )
//avcodec_alloc_frame is an old name
int main(int argc, char *argv[]) {
// Initalizing these to NULL prevents segfaults!
AVFormatContext *pFormatCtx = NULL;
int i, videoStream;
AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVPacket packet;
int frameFinished;
int numBytes;
struct SwsContext *sws_ctx = NULL;
int frame_index=0;
if(argc < 2) {
printf("Please provide a movie file\n");
return -1;
}
// Register all formats and codecs
av_register_all();
//initialize network video
avformat_network_init();
int errorStatus = 0;
char errorLog[128] = { 0 };
av_log_set_level(AV_LOG_TRACE);
//------------------ This is for local video file --------------
// Open video file
if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
return -1; // Couldn't find stream information
// Find the first video stream
videoStream=-1;
for(i=0; inb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
// Dump information about file onto standard error
av_dump_format(pFormatCtx, 0, argv[1], 0);
if(videoStream==-1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtxOrig=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtxOrig->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
// Copy context
pCodecCtx = avcodec_alloc_context3(pCodec);
if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
fprintf(stderr, "Couldn't copy codec context");
return -1; // Error copying codec context
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec, NULL)<0)
return -1; // Could not open codec
// Allocate video frame
pFrame=av_frame_alloc();
// use nvidia codec
AVCodec* en_codec = avcodec_find_encoder_by_name("nvenc");
//AVCodec* en_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
AVCodecContext* nv_codec_context = avcodec_alloc_context3(en_codec);
nv_codec_context->bit_rate = 1244179;
nv_codec_context->width = 1920;
nv_codec_context->height = 1080;
nv_codec_context->time_base.num = 1;
nv_codec_context->time_base.den = 30;
nv_codec_context->gop_size = 10;
nv_codec_context->max_b_frames = 1;
nv_codec_context->keyint_min = 1;
nv_codec_context->i_quant_factor = (float)0.71;
nv_codec_context->b_frame_strategy = 20;
nv_codec_context->qcompress = (float)0.6;
nv_codec_context->qmin = 20;
nv_codec_context->qmax = 51;
nv_codec_context->max_qdiff = 4;
nv_codec_context->refs = 4;
nv_codec_context->trellis = 1;
nv_codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
//nv_codec_context->codec_id = AV_CODEC_ID_H264;
//nv_codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
if (avcodec_open2(nv_codec_context, en_codec,NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}else printf("\nH264 codec opened\n");
/******stream*******/
string m_output("rtp://147.8.179.229:6666");
AVFormatContext* m_formatContext = NULL;
AVStream* m_stream = NULL;
if (avformat_alloc_output_context2(&m_formatContext, NULL, "H264", m_output.c_str()) < 0) {
cerr << "Cannot allocate output context: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
//AVCodec* tmp_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
m_stream = avformat_new_stream(m_formatContext, en_codec);
if (!m_stream) {
cerr << "Cannot create a new stream: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
av_dump_format(m_formatContext, 0, m_output.c_str(), 1);
m_stream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
m_stream->codec->width = 1920;
m_stream->codec->height = 1080;
//m_stream->codec->codec_id = AV_CODEC_ID_H264;
m_stream->codec->bit_rate = 40000;
m_stream->codec->time_base.den = 30;
m_stream->codec->time_base.num = 1;
m_stream->time_base.den = 30;
m_stream->time_base.num = 1;
m_stream->codec->codec_tag = 0;
if (m_formatContext->oformat->flags & AVFMT_GLOBALHEADER)
m_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
if (!(m_formatContext->oformat->flags & AVFMT_NOFILE))
errorStatus = avio_open(&m_formatContext->pb, m_output.c_str(), AVIO_FLAG_WRITE);
if ((errorStatus) < 0) {
cerr << "Cannot open output: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
if (avformat_write_header(m_formatContext, NULL) < 0) {
cerr << "Cannot write header to stream: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
/******stream*******/
FILE *fp_yuv = NULL;
fp_yuv=fopen("output.yuv","wb+");
// Read frames and save first five frames to disk
i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
// Did we get a video frame?
if(frameFinished) {
AVPacket Outpacket;
int got_packet_ptr;
av_init_packet(&Outpacket);
Outpacket.data = NULL;
Outpacket.size = 0;
int out_size = avcodec_encode_video2(nv_codec_context, &Outpacket, pFrame, &got_packet_ptr);
Outpacket.pts = av_rescale_q(pFrame->pts, m_stream->codec->time_base, m_stream->time_base);
fwrite(Outpacket.data,1,Outpacket.size,fp_yuv);
//av_write_frame(m_formatContext, &Outpacket);
av_interleaved_write_frame(m_formatContext, &Outpacket);
//Free the packet that was allocated by encoder
av_packet_unref(&Outpacket);
}
}
// Free the packet that was allocated by av_read_frame
av_packet_unref(&packet);
}
fclose(fp_yuv);
// Free the YUV frame
av_frame_free(&pFrame);
// Close the codecs
avcodec_close(pCodecCtx);
avcodec_close(pCodecCtxOrig);
avcodec_close(m_stream->codec);
avformat_free_context(m_formatContext);
// Close the video file
avformat_close_input(&pFormatCtx);
return 0;
}
</string></iostream>Above is my code for decoding a .mp4 file, outputting to a network stream through RTP. But it does not work. On the client side, I call VLC library to decode the stream ( VlcMedia(url, _instance) where url is rtp/h264 ://@147.8.179.229:6666 ). When I change the protocal to UDP, it works. However, it is not stable. The frames jumps and probably the frame lost or something wrong with the pts setting ?
UPDATED :
When I use UDP, I got this error message.
When I use RTP, I got this error message :
Not sure about how to solve this problem, Can anyone help ? Thanks very much.
-
FFmpeg H264 RTP streaming error
20 juillet 2016, par JohnnylinCan anyone show me an example code of how to use FFmpeg to encode a ".mp4" video and then output to network stream using RTP(rtp ://127.0.0.1:6666). I have searched for it on google but most of them are just command line. Thanks very much.
UPDATED :
extern "C"{
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libswscale></libswscale>swscale.h>
#include <libavutil></libavutil>avutil.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>time.h>
}
#include
#include <iostream>
#include <string>
using namespace std;
// compatibility with newer API
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc avcodec_alloc_frame
#define av_frame_free avcodec_free_frame
#endif
#define RNDTO2(X) ( ( (X) & 0xFFFFFFFE ))
#define RNDTO32(X) ( ( (X) % 32 ) ? ( ( (X) + 32 ) & 0xFFFFFFE0 ) : (X) )
//avcodec_alloc_frame is an old name
int main(int argc, char *argv[]) {
// Initalizing these to NULL prevents segfaults!
AVFormatContext *pFormatCtx = NULL;
int i, videoStream;
AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVPacket packet;
int frameFinished;
int numBytes;
struct SwsContext *sws_ctx = NULL;
int frame_index=0;
if(argc < 2) {
printf("Please provide a movie file\n");
return -1;
}
// Register all formats and codecs
av_register_all();
//initialize network video
avformat_network_init();
int errorStatus = 0;
char errorLog[128] = { 0 };
av_log_set_level(AV_LOG_TRACE);
//------------------ This is for local video file --------------
// Open video file
if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
return -1; // Couldn't find stream information
// Find the first video stream
videoStream=-1;
for(i=0; inb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
// Dump information about file onto standard error
av_dump_format(pFormatCtx, 0, argv[1], 0);
if(videoStream==-1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtxOrig=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtxOrig->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
// Copy context
pCodecCtx = avcodec_alloc_context3(pCodec);
if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
fprintf(stderr, "Couldn't copy codec context");
return -1; // Error copying codec context
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec, NULL)<0)
return -1; // Could not open codec
// Allocate video frame
pFrame=av_frame_alloc();
// use nvidia codec
AVCodec* en_codec = avcodec_find_encoder_by_name("nvenc");
//AVCodec* en_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
AVCodecContext* nv_codec_context = avcodec_alloc_context3(en_codec);
nv_codec_context->bit_rate = 1244179;
nv_codec_context->width = 1920;
nv_codec_context->height = 1080;
nv_codec_context->time_base.num = 1;
nv_codec_context->time_base.den = 30;
nv_codec_context->gop_size = 10;
nv_codec_context->max_b_frames = 1;
nv_codec_context->keyint_min = 1;
nv_codec_context->i_quant_factor = (float)0.71;
nv_codec_context->b_frame_strategy = 20;
nv_codec_context->qcompress = (float)0.6;
nv_codec_context->qmin = 20;
nv_codec_context->qmax = 51;
nv_codec_context->max_qdiff = 4;
nv_codec_context->refs = 4;
nv_codec_context->trellis = 1;
nv_codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
//nv_codec_context->codec_id = AV_CODEC_ID_H264;
//nv_codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
if (avcodec_open2(nv_codec_context, en_codec,NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}else printf("\nH264 codec opened\n");
/******stream*******/
string m_output("rtp://147.8.179.229:6666");
AVFormatContext* m_formatContext = NULL;
AVStream* m_stream = NULL;
if (avformat_alloc_output_context2(&m_formatContext, NULL, "H264", m_output.c_str()) < 0) {
cerr << "Cannot allocate output context: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
//AVCodec* tmp_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
m_stream = avformat_new_stream(m_formatContext, en_codec);
if (!m_stream) {
cerr << "Cannot create a new stream: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
av_dump_format(m_formatContext, 0, m_output.c_str(), 1);
m_stream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
m_stream->codec->width = 1920;
m_stream->codec->height = 1080;
//m_stream->codec->codec_id = AV_CODEC_ID_H264;
m_stream->codec->bit_rate = 40000;
m_stream->codec->time_base.den = 30;
m_stream->codec->time_base.num = 1;
m_stream->time_base.den = 30;
m_stream->time_base.num = 1;
m_stream->codec->codec_tag = 0;
if (m_formatContext->oformat->flags & AVFMT_GLOBALHEADER)
m_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
if (!(m_formatContext->oformat->flags & AVFMT_NOFILE))
errorStatus = avio_open(&m_formatContext->pb, m_output.c_str(), AVIO_FLAG_WRITE);
if ((errorStatus) < 0) {
cerr << "Cannot open output: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
if (avformat_write_header(m_formatContext, NULL) < 0) {
cerr << "Cannot write header to stream: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
/******stream*******/
FILE *fp_yuv = NULL;
fp_yuv=fopen("output.yuv","wb+");
// Read frames and save first five frames to disk
i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
// Did we get a video frame?
if(frameFinished) {
AVPacket Outpacket;
int got_packet_ptr;
av_init_packet(&Outpacket);
Outpacket.data = NULL;
Outpacket.size = 0;
int out_size = avcodec_encode_video2(nv_codec_context, &Outpacket, pFrame, &got_packet_ptr);
Outpacket.pts = av_rescale_q(pFrame->pts, m_stream->codec->time_base, m_stream->time_base);
fwrite(Outpacket.data,1,Outpacket.size,fp_yuv);
//av_write_frame(m_formatContext, &Outpacket);
av_interleaved_write_frame(m_formatContext, &Outpacket);
//Free the packet that was allocated by encoder
av_packet_unref(&Outpacket);
}
}
// Free the packet that was allocated by av_read_frame
av_packet_unref(&packet);
}
fclose(fp_yuv);
// Free the YUV frame
av_frame_free(&pFrame);
// Close the codecs
avcodec_close(pCodecCtx);
avcodec_close(pCodecCtxOrig);
avcodec_close(m_stream->codec);
avformat_free_context(m_formatContext);
// Close the video file
avformat_close_input(&pFormatCtx);
return 0;
}
</string></iostream>Above is my code for decoding a .mp4 file, outputting to a network stream through RTP. But it does not work. On the client side, I call VLC library to decode the stream ( VlcMedia(url, _instance) where url is rtp/h264 ://@147.8.179.229:6666 ). When I change the protocal to UDP, it works. However, it is not stable. The frames jumps and probably the frame lost or something wrong with the pts setting ?
When I use UDP, I got this error message :
When I change it to RTP, I got this error message :
Not sure about how to solve this problem, Can anyone help ? Thanks very much.
-
FFmpeg RTP streaming
19 juillet 2016, par JohnnylinCan anyone show me an example code of how to use FFmpeg to encode a ".mp4" video and then output to network stream using RTP(rtp ://127.0.0.1:6666). I have searched for it on google but most of them are just command line. Thanks very much.
UPDATED :
extern "C"{
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libswscale></libswscale>swscale.h>
#include <libavutil></libavutil>avutil.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>time.h>
}
#include
#include <iostream>
#include <string>
using namespace std;
// compatibility with newer API
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
#define av_frame_alloc avcodec_alloc_frame
#define av_frame_free avcodec_free_frame
#endif
#define RNDTO2(X) ( ( (X) & 0xFFFFFFFE ))
#define RNDTO32(X) ( ( (X) % 32 ) ? ( ( (X) + 32 ) & 0xFFFFFFE0 ) : (X) )
//avcodec_alloc_frame is an old name
int main(int argc, char *argv[]) {
// Initalizing these to NULL prevents segfaults!
AVFormatContext *pFormatCtx = NULL;
int i, videoStream;
AVCodecContext *pCodecCtxOrig = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVPacket packet;
int frameFinished;
int numBytes;
struct SwsContext *sws_ctx = NULL;
int frame_index=0;
if(argc < 2) {
printf("Please provide a movie file\n");
return -1;
}
// Register all formats and codecs
av_register_all();
//initialize network video
avformat_network_init();
int errorStatus = 0;
char errorLog[128] = { 0 };
av_log_set_level(AV_LOG_TRACE);
//------------------ This is for local video file --------------
// Open video file
if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
return -1; // Couldn't open file
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
return -1; // Couldn't find stream information
// Find the first video stream
videoStream=-1;
for(i=0; inb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
// Dump information about file onto standard error
av_dump_format(pFormatCtx, 0, argv[1], 0);
if(videoStream==-1)
return -1; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtxOrig=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtxOrig->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
// Copy context
pCodecCtx = avcodec_alloc_context3(pCodec);
if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) {
fprintf(stderr, "Couldn't copy codec context");
return -1; // Error copying codec context
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec, NULL)<0)
return -1; // Could not open codec
// Allocate video frame
pFrame=av_frame_alloc();
// use nvidia codec
AVCodec* en_codec = avcodec_find_encoder_by_name("nvenc");
//AVCodec* en_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
AVCodecContext* nv_codec_context = avcodec_alloc_context3(en_codec);
nv_codec_context->bit_rate = 1244179;
nv_codec_context->width = 1920;
nv_codec_context->height = 1080;
nv_codec_context->time_base.num = 1;
nv_codec_context->time_base.den = 30;
nv_codec_context->gop_size = 10;
nv_codec_context->max_b_frames = 1;
nv_codec_context->keyint_min = 1;
nv_codec_context->i_quant_factor = (float)0.71;
nv_codec_context->b_frame_strategy = 20;
nv_codec_context->qcompress = (float)0.6;
nv_codec_context->qmin = 20;
nv_codec_context->qmax = 51;
nv_codec_context->max_qdiff = 4;
nv_codec_context->refs = 4;
nv_codec_context->trellis = 1;
nv_codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
//nv_codec_context->codec_id = AV_CODEC_ID_H264;
//nv_codec_context->codec_type = AVMEDIA_TYPE_VIDEO;
if (avcodec_open2(nv_codec_context, en_codec,NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}else printf("\nH264 codec opened\n");
/******stream*******/
string m_output("rtp://147.8.179.229:6666");
AVFormatContext* m_formatContext = NULL;
AVStream* m_stream = NULL;
if (avformat_alloc_output_context2(&m_formatContext, NULL, "H264", m_output.c_str()) < 0) {
cerr << "Cannot allocate output context: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
//AVCodec* tmp_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
m_stream = avformat_new_stream(m_formatContext, en_codec);
if (!m_stream) {
cerr << "Cannot create a new stream: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
av_dump_format(m_formatContext, 0, m_output.c_str(), 1);
m_stream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
m_stream->codec->width = 1920;
m_stream->codec->height = 1080;
//m_stream->codec->codec_id = AV_CODEC_ID_H264;
m_stream->codec->bit_rate = 40000;
m_stream->codec->time_base.den = 30;
m_stream->codec->time_base.num = 1;
m_stream->time_base.den = 30;
m_stream->time_base.num = 1;
m_stream->codec->codec_tag = 0;
if (m_formatContext->oformat->flags & AVFMT_GLOBALHEADER)
m_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
if (!(m_formatContext->oformat->flags & AVFMT_NOFILE))
errorStatus = avio_open(&m_formatContext->pb, m_output.c_str(), AVIO_FLAG_WRITE);
if ((errorStatus) < 0) {
cerr << "Cannot open output: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
if (avformat_write_header(m_formatContext, NULL) < 0) {
cerr << "Cannot write header to stream: "
<< av_make_error_string(errorLog, 128, errorStatus) << endl;
return -1;
}
/******stream*******/
FILE *fp_yuv = NULL;
fp_yuv=fopen("output.yuv","wb+");
// Read frames and save first five frames to disk
i=0;
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
// Did we get a video frame?
if(frameFinished) {
AVPacket Outpacket;
int got_packet_ptr;
av_init_packet(&Outpacket);
Outpacket.data = NULL;
Outpacket.size = 0;
int out_size = avcodec_encode_video2(nv_codec_context, &Outpacket, pFrame, &got_packet_ptr);
Outpacket.pts = av_rescale_q(pFrame->pts, m_stream->codec->time_base, m_stream->time_base);
fwrite(Outpacket.data,1,Outpacket.size,fp_yuv);
//av_write_frame(m_formatContext, &Outpacket);
av_interleaved_write_frame(m_formatContext, &Outpacket);
//Free the packet that was allocated by encoder
av_packet_unref(&Outpacket);
}
}
// Free the packet that was allocated by av_read_frame
av_packet_unref(&packet);
}
fclose(fp_yuv);
// Free the YUV frame
av_frame_free(&pFrame);
// Close the codecs
avcodec_close(pCodecCtx);
avcodec_close(pCodecCtxOrig);
avcodec_close(m_stream->codec);
avformat_free_context(m_formatContext);
// Close the video file
avformat_close_input(&pFormatCtx);
return 0;
}
</string></iostream>Above is my code for decoding a .mp4 file, outputting to a network stream through RTP. But it does not work. On the client side, I call VLC library to decode the stream ( VlcMedia(url, _instance) where url is rtp/h264 ://@147.8.179.229:6666 ). When I change the protocal to UDP, it works. However, it is not stable. The frames jumps and probably the frame lost or something wrong with the pts setting ?
Can anyone help ? Thanks very much.