
Recherche avancée
Médias (91)
-
Les Miserables
9 décembre 2019, par
Mis à jour : Décembre 2019
Langue : français
Type : Textuel
-
VideoHandle
8 novembre 2019, par
Mis à jour : Novembre 2019
Langue : français
Type : Video
-
Somos millones 1
21 juillet 2014, par
Mis à jour : Juin 2015
Langue : français
Type : Video
-
Un test - mauritanie
3 avril 2014, par
Mis à jour : Avril 2014
Langue : français
Type : Textuel
-
Pourquoi Obama lit il mes mails ?
4 février 2014, par
Mis à jour : Février 2014
Langue : français
-
IMG 0222
6 octobre 2013, par
Mis à jour : Octobre 2013
Langue : français
Type : Image
Autres articles (30)
-
MediaSPIP Player : problèmes potentiels
22 février 2011, parLe lecteur ne fonctionne pas sur Internet Explorer
Sur Internet Explorer (8 et 7 au moins), le plugin utilise le lecteur Flash flowplayer pour lire vidéos et son. Si le lecteur ne semble pas fonctionner, cela peut venir de la configuration du mod_deflate d’Apache.
Si dans la configuration de ce module Apache vous avez une ligne qui ressemble à la suivante, essayez de la supprimer ou de la commenter pour voir si le lecteur fonctionne correctement : /** * GeSHi (C) 2004 - 2007 Nigel McNie, (...) -
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 (...) -
Librairies et logiciels spécifiques aux médias
10 décembre 2010, parPour un fonctionnement correct et optimal, plusieurs choses sont à prendre en considération.
Il est important, après avoir installé apache2, mysql et php5, d’installer d’autres logiciels nécessaires dont les installations sont décrites dans les liens afférants. Un ensemble de librairies multimedias (x264, libtheora, libvpx) utilisées pour l’encodage et le décodage des vidéos et sons afin de supporter le plus grand nombre de fichiers possibles. Cf. : ce tutoriel ; FFMpeg avec le maximum de décodeurs et (...)
Sur d’autres sites (2639)
-
Downscaling a video from 1080p to 480p using swscale and encoding to x265 gives a glitched output
5 mai 2023, par lokit khemkaI am basically first scaling a frame and then sending the frame to the encoder as below :


scaled_frame->pts = input_frame->pts;
scaled_frame->pkt_dts = input_frame->pkt_dts;
scaled_frame->pict_type = input_frame->pict_type;
sws_scale_frame(encoder->sws_ctx, scaled_frame, input_frame);
if (encode_video(decoder, encoder, scaled_frame))
 return -1;



The scaling context is configured as :


scaled_frame->width = 854;
scaled_frame->height=480; 
encoder->sws_ctx = sws_getContext(1920, 1080,
 decoder->video_avcc->pix_fmt, 
 scaled_frame->width, scaled_frame->height, decoder->video_avcc->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL );
 if (!encoder->sws_ctx){logging("Cannot Create Scaling Context."); return -1;}



The encoder is configured as :


encoder_sc->video_avcc->height = decoder_ctx->height; //1080
 encoder_sc->video_avcc->width = decoder_ctx->width; //1920
 encoder_sc->video_avcc->bit_rate = 2 * 1000 * 1000;
 encoder_sc->video_avcc->rc_buffer_size = 4 * 1000 * 1000;
 encoder_sc->video_avcc->rc_max_rate = 2 * 1000 * 1000;
 encoder_sc->video_avcc->rc_min_rate = 2.5 * 1000 * 1000;

 encoder_sc->video_avcc->time_base = av_inv_q(input_framerate);
 encoder_sc->video_avs->time_base = encoder_sc->video_avcc->time_base;



When I get the output, the output video is 1080p and I have glitches like :


I changed the encoder avcc resolution to 480p (854 x 480). However, that is causing the video to get sliced to the top quarter of the original frame.
I am new to FFMPEG and video processing in general.


EDIT : I am adding the minimal reproducible code sample. However, it is really long because I need to include code for decoding, scaling and then encoding because the possible error is either in scaling or encoding :


#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>timestamp.h>
#include <libavutil></libavutil>opt.h>
#include <libswscale></libswscale>swscale.h>

#include 
#include 

typedef struct StreamingContext{
 AVFormatContext* avfc;
 AVCodec *video_avc;
 AVCodec *audio_avc;
 AVStream *video_avs;
 AVStream *audio_avs;
 AVCodecContext *video_avcc;
 AVCodecContext *audio_avcc;
 int video_index;
 int audio_index;
 char* filename;
 struct SwsContext *sws_ctx;
}StreamingContext;


typedef struct StreamingParams{
 char copy_video;
 char copy_audio;
 char *output_extension;
 char *muxer_opt_key;
 char *muxer_opt_value;
 char *video_codec;
 char *audio_codec;
 char *codec_priv_key;
 char *codec_priv_value;
}StreamingParams;

void logging(const char *fmt, ...)
{
 va_list args;
 fprintf(stderr, "LOG: ");
 va_start(args, fmt);
 vfprintf(stderr, fmt, args);
 va_end(args);
 fprintf(stderr, "\n");
}

int fill_stream_info(AVStream *avs, AVCodec **avc, AVCodecContext **avcc)
{
 *avc = avcodec_find_decoder(avs->codecpar->codec_id);
 if (!*avc)
 {
 logging("Failed to find the codec.\n");
 return -1;
 }

 *avcc = avcodec_alloc_context3(*avc);
 if (!*avcc)
 {
 logging("Failed to alloc memory for codec context.");
 return -1;
 }

 if (avcodec_parameters_to_context(*avcc, avs->codecpar) < 0)
 {
 logging("Failed to fill Codec Context.");
 return -1;
 }

 if (avcodec_open2(*avcc, *avc, NULL) < 0)
 {
 logging("Failed to open Codec.");
 return -1;
 }

 return 0;
}

int open_media(const char *in_filename, AVFormatContext **avfc)
{
 *avfc = avformat_alloc_context();

 if (!*avfc)
 {
 logging("Failed to Allocate Memory for Format Context");
 return -1;
 }

 if (avformat_open_input(avfc, in_filename, NULL, NULL) != 0)
 {
 logging("Failed to open input file %s", in_filename);
 return -1;
 }

 if (avformat_find_stream_info(*avfc, NULL) < 0)
 {
 logging("Failed to get Stream Info.");
 return -1;
 }
}

int prepare_decoder(StreamingContext *sc)
{
 for (int i = 0; i < sc->avfc->nb_streams; i++)
 {
 if (sc->avfc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
 {
 sc->video_avs = sc->avfc->streams[i];
 sc->video_index = i;

 if (fill_stream_info(sc->video_avs, &sc->video_avc, &sc->video_avcc))
 {
 return -1;
 }
 }
 else
 {
 logging("Skipping Streams other than Video.");
 }
 }
 return 0;
}

int prepare_video_encoder(StreamingContext *encoder_sc, AVCodecContext *decoder_ctx, AVRational input_framerate,
 StreamingParams sp)
{
 encoder_sc->video_avs = avformat_new_stream(encoder_sc->avfc, NULL);
 encoder_sc->video_avc = avcodec_find_encoder_by_name(sp.video_codec);
 if (!encoder_sc->video_avc)
 {
 logging("Cannot find the Codec.");
 return -1;
 }

 encoder_sc->video_avcc = avcodec_alloc_context3(encoder_sc->video_avc);
 if (!encoder_sc->video_avcc)
 {
 logging("Could not allocate memory for Codec Context.");
 return -1;
 }

 av_opt_set(encoder_sc->video_avcc->priv_data, "preset", "fast", 0);
 if (sp.codec_priv_key && sp.codec_priv_value)
 av_opt_set(encoder_sc->video_avcc->priv_data, sp.codec_priv_key, sp.codec_priv_value, 0);

 encoder_sc->video_avcc->height = decoder_ctx->height;
 encoder_sc->video_avcc->width = decoder_ctx->width;
 encoder_sc->video_avcc->sample_aspect_ratio = decoder_ctx->sample_aspect_ratio;

 if (encoder_sc->video_avc->pix_fmts)
 encoder_sc->video_avcc->pix_fmt = encoder_sc->video_avc->pix_fmts[0];
 else
 encoder_sc->video_avcc->pix_fmt = decoder_ctx->pix_fmt;

 encoder_sc->video_avcc->bit_rate = 2 * 1000 * 1000;
 encoder_sc->video_avcc->rc_buffer_size = 4 * 1000 * 1000;
 encoder_sc->video_avcc->rc_max_rate = 2 * 1000 * 1000;
 encoder_sc->video_avcc->rc_min_rate = 2.5 * 1000 * 1000;

 encoder_sc->video_avcc->time_base = av_inv_q(input_framerate);
 encoder_sc->video_avs->time_base = encoder_sc->video_avcc->time_base;

 

 if (avcodec_open2(encoder_sc->video_avcc, encoder_sc->video_avc, NULL) < 0)
 {
 logging("Could not open the Codec.");
 return -1;
 }
 avcodec_parameters_from_context(encoder_sc->video_avs->codecpar, encoder_sc->video_avcc);
 return 0;
}

int encode_video(StreamingContext *decoder, StreamingContext *encoder, AVFrame *input_frame)
{
 if (input_frame)
 input_frame->pict_type = AV_PICTURE_TYPE_NONE;

 AVPacket *output_packet = av_packet_alloc();
 if (!output_packet)
 {
 logging("Could not allocate memory for Output Packet.");
 return -1;
 }

 int response = avcodec_send_frame(encoder->video_avcc, input_frame);

 while (response >= 0)
 {
 response = avcodec_receive_packet(encoder->video_avcc, output_packet);
 if (response == AVERROR(EAGAIN) || response == AVERROR_EOF)
 {
 break;
 }
 else if (response < 0)
 {
 logging("Error while receiving packet from encoder: %s", av_err2str(response));
 return -1;
 }

 output_packet->stream_index = decoder->video_index;
 output_packet->duration = encoder->video_avs->time_base.den / encoder->video_avs->time_base.num / decoder->video_avs->avg_frame_rate.num * decoder->video_avs->avg_frame_rate.den;

 av_packet_rescale_ts(output_packet, decoder->video_avs->time_base, encoder->video_avs->time_base);
 response = av_interleaved_write_frame(encoder->avfc, output_packet);
 if (response != 0)
 {
 logging("Error %d while receiving packet from decoder: %s", response, av_err2str(response));
 return -1;
 }
 }

 av_packet_unref(output_packet);
 av_packet_free(&output_packet);

 return 0;
}

int transcode_video(StreamingContext *decoder, StreamingContext *encoder, AVPacket *input_packet, AVFrame *input_frame, AVFrame *scaled_frame)
{
 int response = avcodec_send_packet(decoder->video_avcc, input_packet);
 if (response < 0)
 {
 logging("Error while sending the Packet to Decoder: %s", av_err2str(response));
 return response;
 }

 while (response >= 0)
 {
 response = avcodec_receive_frame(decoder->video_avcc, input_frame);
 
 if (response == AVERROR(EAGAIN) || response == AVERROR_EOF)
 {
 break;
 }
 else if (response < 0)
 {
 logging("Error while receiving frame from Decoder: %s", av_err2str(response));
 return response;
 }
 if (response >= 0)
 {
 scaled_frame->pts = input_frame->pts;
 scaled_frame->pkt_dts = input_frame->pkt_dts;
 scaled_frame->pict_type = input_frame->pict_type;
 sws_scale_frame(encoder->sws_ctx, scaled_frame, input_frame);
 if (encode_video(decoder, encoder, scaled_frame))
 return -1;
 }

 av_frame_unref(input_frame);
 }
 return 0;
}

int main(int argc, char *argv[])
{
 StreamingParams sp = {0};
 sp.copy_audio = 1;
 sp.copy_video = 0;
 sp.video_codec = "libx265";


 StreamingContext *decoder = (StreamingContext *)calloc(1, sizeof(StreamingContext));
 decoder->filename = argv[1];

 StreamingContext *encoder = (StreamingContext *)calloc(1, sizeof(StreamingContext));
 encoder->filename = argv[2];

 if (sp.output_extension)
 {
 strcat(encoder->filename, sp.output_extension);
 }

 if (open_media(decoder->filename, &decoder->avfc))
 return -1;
 if (prepare_decoder(decoder))
 return -1;

 avformat_alloc_output_context2(&encoder->avfc, NULL, NULL, encoder->filename);
 if (!encoder->avfc)
 {
 logging("Could not allocate memory for output Format Context.");
 return -1;
 }

 AVRational input_framerate = av_guess_frame_rate(decoder->avfc, decoder->video_avs, NULL);
 prepare_video_encoder(encoder, decoder->video_avcc, input_framerate, sp);


 if (encoder->avfc->oformat->flags & AVFMT_GLOBALHEADER)
 encoder->avfc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

 if (!(encoder->avfc->oformat->flags & AVFMT_NOFILE))
 {
 if (avio_open(&encoder->avfc->pb, encoder->filename, AVIO_FLAG_WRITE) < 0)
 {
 logging("could not open the output file");
 return -1;
 }
 }

 AVDictionary *muxer_opts = NULL;

 if (sp.muxer_opt_key && sp.muxer_opt_value)
 {
 av_dict_set(&muxer_opts, sp.muxer_opt_key, sp.muxer_opt_value, 0);
 }

 if (avformat_write_header(encoder->avfc, &muxer_opts) < 0)
 {
 logging("an error occurred when opening output file");
 return -1;
 }

 AVFrame *input_frame = av_frame_alloc();
 AVFrame *scaled_frame = av_frame_alloc();
 if (!input_frame || !scaled_frame)
 {
 logging("Failed to allocate memory for AVFrame");
 return -1;
 }

 // scaled_frame->format = AV_PIX_FMT_YUV420P;
 scaled_frame->width = 854;
 scaled_frame->height=480; 

 //Creating Scaling Context
 encoder->sws_ctx = sws_getContext(1920, 1080,
 decoder->video_avcc->pix_fmt, 
 scaled_frame->width, scaled_frame->height, decoder->video_avcc->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL );
 if (!encoder->sws_ctx){logging("Cannot Create Scaling Context."); return -1;}


 AVPacket *input_packet = av_packet_alloc();
 if (!input_packet)
 {
 logging("Failed to allocate memory for AVPacket.");
 return -1;
 }

 while (av_read_frame(decoder->avfc, input_packet) >= 0)
 {
 if (decoder->avfc->streams[input_packet->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
 {
 if (transcode_video(decoder, encoder, input_packet, input_frame, scaled_frame))
 return -1;
 av_packet_unref(input_packet);
 }
 else
 {
 logging("Ignoring all nonvideo packets.");
 }
 }

 if (encode_video(decoder, encoder, NULL))
 return -1;

 av_write_trailer(encoder->avfc);

 if (muxer_opts != NULL)
 {
 av_dict_free(&muxer_opts);
 muxer_opts = NULL;
 }

 if (input_frame != NULL)
 {
 av_frame_free(&input_frame);
 input_frame = NULL;
 }

 if (input_packet != NULL)
 {
 av_packet_free(&input_packet);
 input_packet = NULL;
 }

 avformat_close_input(&decoder->avfc);

 avformat_free_context(decoder->avfc);
 decoder->avfc = NULL;
 avformat_free_context(encoder->avfc);
 encoder->avfc = NULL;

 avcodec_free_context(&decoder->video_avcc);
 decoder->video_avcc = NULL;
 avcodec_free_context(&decoder->audio_avcc);
 decoder->audio_avcc = NULL;

 free(decoder);
 decoder = NULL;
 free(encoder);
 encoder = NULL;

 return 0;
}



The video I am using for testing is available at the repo : https://github.com/leandromoreira/ffmpeg-libav-tutorial


The file name is small_bunny_1080p_60fps.mp4


-
FFMPEG avformat_open_input returning AVERROR_INVALIDDATA [on hold]
10 août 2016, par Victor.dMdBI’m trying to use ffmpeg to take in video data from a buffer but keep on getting the same error.
I’ve implemented the same code as described here :
http://www.ffmpeg.org/doxygen/trunk/doc_2examples_2avio_reading_8c-example.htmlHere is the code (I’ve tried removing the unnecessary bits)
VideoInputFile *video_input_file;
VideoDataConf *video_data_conf;
video_data_conf->width = 1920;
video_data_conf->height = 1080;
int vbuf_size = 9 * cmd_data_ptr->video_data_conf.width * cmd_data_ptr->video_data_conf.height + 10000;
uint8_t *buffer = (uint8_t *) av_malloc(vbuf_size);
video_data_conf->input_ptr.ptr = buffer;
video_data_conf->input_ptr.size = vbuf_size;
strncpy(video_data_conf->filename, "localmem");
video_input_file->vbuf_size = 9 * video_data_conf->width * video_data_conf->height + 10000;
video_input_file->vbuf = (uint8_t *) av_malloc(video_input_file->vbuf_size);
video_input_file->av_io_ctx = avio_alloc_context(video_input_file->vbuf, video_input_file->vbuf_size, 0, &video_data_conf->input_ptr, &read_function, NULL, NULL);
if ( !video_input_file->av_io_ctx ) {
fprintf(stdout,"Failed to create the buffer avio context\n");
}
video_input_file->av_fmt_ctx = avformat_alloc_context();
if ( !video_input_file->av_fmt_ctx ) {
printf(stdout,"Failed to create the video avio context\n");
}
video_input_file->av_fmt_ctx->pb = video_input_file->av_io_ctx;
open_res = avformat_open_input(&video_input_file->av_fmt_ctx, video_data_conf->filename, NULL, NULL);Read function :
static int read_function(void* opaque, uint8_t* buf, int buf_size) {
BufferData *bd = (BufferData *) opaque;
buf_size = FFMIN(buf_size, bd->size);
memcpy(buf, bd->ptr, buf_size);
bd->ptr += buf_size;
bd->size -= buf_size;
return buf_size;
}BufferData structure
typedef struct {
uint8_t *ptr;
size_t size; ///< size left in the buffer
} BufferData;It starts to work if I initialise the buffer and vbuf_size with a real file like so :
uint8_t *buffer;
size_t vbuf_size;
av_file_map("/path/to/image.png", &buffer, &vbuf_size, 0, NULL); -
libvlc ffmpeg : No seek in mpegts h264 stream
26 juillet 2016, par ElDoradoI am using ffmpeg to record video input from GDI (windows screen recorder) to view it later using VLC (via ActiveX plugin) + ffmpeg to decode it.
Right now seeking in video is not working in VLC via plugin (which is critical). VLC player itself provide seeking, but it is more like byte position seeking (on I- frames which are larger than other frames it makes larger steps on horizontal scroll and also there are no timestamps).
Encoder is opened with next defaults :
avformat_alloc_output_context2(&outputContext, NULL, "mpegts", "test.mpg");
outputFormat = outputContext->oformat;
encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
outputStream = avformat_new_stream(outputContext, encoder);
outputStream->id = outputContext->nb_streams - 1;
encoderContext = outputStream->codec;
encoderContext->bit_rate = bitrate; // 800000 by default
encoderContext->rc_max_rate = bitrate;
encoderContext->width = imageWidth; // 1920
encoderContext->height = imageHeight; // 1080
encoderContext->time_base.num = 1;
encoderContext->time_base.den = fps; // 25 by default
encoderContext->gop_size = fps;
encoderContext->keyint_min = fps;
encoderContext->max_b_frames = 0;
encoderContext->pix_fmt = AV_PIX_FMT_YUV420P;
outputStream->time_base = encoderContext->time_base;
avcodec_open2(encoderContext, encoder, NULL);Recording is done this way :
// my impl of GDI recorder, returning AVFrame with only data and linesize filled.
AVFrame* tmp_frame = impl_->recorder->acquireFrame();
// converting RGB -> YUV420
sws_scale(impl_->scaleContext, tmp_frame->data, tmp_frame->linesize, 0, impl_->frame->height, impl_->frame->data, impl_->frame->linesize);
// pts variable is calculated by using QueryPerformanceCounter form WinAPI. It is strictly increasing
impl_->frame->pts = pts;
avcodec_encode_video2(impl_->encoderContext, impl_->packet, impl_->frame, &out_size);
if (out_size) {
impl_->packet->pts = pts;
impl_->packet->dts = pts;
impl_->packet->duration = 1; // here it is! It is set but has no effect
av_packet_rescale_ts(impl_->packet, impl_->encoderContext->time_base, impl_->outputStream->time_base);
// here pts = 3600*pts, dts = 3600*pts, duration = 3600 what I consider to be legit in terms of milliseconds
impl_->packet->stream_index = impl_->outputStream->index;
av_interleaved_write_frame(impl_->outputContext, impl_->packet);
av_packet_unref(impl_->packet);
out_size = 0;
}ffprobe is providing next info on frames :
[FRAME]
media_type=video
stream_index=0
key_frame=1
pkt_pts=3600
pkt_pts_time=0:00:00.040000
pkt_dts=3600
pkt_dts_time=0:00:00.040000
best_effort_timestamp=3600
best_effort_timestamp_time=0:00:00.040000
pkt_duration=N/A
pkt_duration_time=N/A
pkt_pos=564
pkt_size=97.018555 Kibyte
width=1920
height=1080
pix_fmt=yuv420p
sample_aspect_ratio=N/A
pict_type=I
coded_picture_number=0
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
[/FRAME]I believe that problem is in
pkt_duration
variable, though it was set.
What I am doing wrong in recording so I can’t seek in video ?P.S. on other videos (also h264) seeking is working in ActiveX VLC plugin.