
Recherche avancée
Médias (1)
-
The pirate bay depuis la Belgique
1er avril 2013, par
Mis à jour : Avril 2013
Langue : français
Type : Image
Autres articles (47)
-
Les autorisations surchargées par les plugins
27 avril 2010, parMediaspip core
autoriser_auteur_modifier() afin que les visiteurs soient capables de modifier leurs informations sur la page d’auteurs -
Les vidéos
21 avril 2011, parComme les documents de type "audio", Mediaspip affiche dans la mesure du possible les vidéos grâce à la balise html5 .
Un des inconvénients de cette balise est qu’elle n’est pas reconnue correctement par certains navigateurs (Internet Explorer pour ne pas le nommer) et que chaque navigateur ne gère en natif que certains formats de vidéos.
Son avantage principal quant à lui est de bénéficier de la prise en charge native de vidéos dans les navigateur et donc de se passer de l’utilisation de Flash et (...) -
Websites made with MediaSPIP
2 mai 2011, parThis page lists some websites based on MediaSPIP.
Sur d’autres sites (4466)
-
libavformat : calling avformat_open_input 2 times results in decoding white frames
25 avril 2017, par explodus- pre build ffmpeg libs format/util/scale
- version 57.56.101
- don’t use any deprecated function
- use the actual style
- av_read_frame -> avcodec_send_packet -> avcodec_receive_frame -> sws_scale
Everything is fine on the first run, but when i wanna load/open another file i only get white frames.
void video::app::flush_cached_frames() {
if (nullptr == avcontext)
return;
if (nullptr == avpicture)
return;
// send an empty packet which instructs the codec to start flushing
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
avcodec_send_packet(avcontext, &pkt);
// drain the codec
while (true) {
int r = avcodec_receive_frame(avcontext, avpicture);
if (r != 0)
break;
}
}
void video::app::close_avi() {
flush_cached_frames();
if (avformat && avstream)
seek_to_frame(0);
avstream = nullptr;
if (avfile)
fclose(avfile);
avfile = nullptr;
if (avcontext)
avcodec_close(avcontext);
avcontext = nullptr;
if (avformat)
avformat_free_context(avformat);
avformat = nullptr;
if (sws_ctx)
sws_freeContext(sws_ctx);
sws_ctx = nullptr;
if (avparser)
av_parser_close(avparser);
avparser = nullptr;
if (avinbuf)
av_free(avinbuf);
avinbuf = nullptr;
}I think i close anything perfectly. Has anyone an idea ?
edit1 : init/load
unsigned video::app::load(const std::string& name) {
_file = name_;
close_avi();
av_register_all();
avcodec_register_all();
av_init_packet(&avpkt);
AVCodecID codec_id = AV_CODEC_ID_H264;
int64_t duration = 0;
double fps = .0;
int ret = 0;
{
av_log_set_level(1);
avfile = fopen(name_.c_str(), "rb");
avformat = avformat_alloc_context();
ret = avformat_open_input(&avformat, name_.c_str(), nullptr, nullptr);
ret = avformat_find_stream_info(avformat, nullptr);
duration = avformat->duration;
avstream = nullptr;
if (avformat->nb_streams == 1) {
avstream = avformat->streams[0];
} else {
avstream = avformat->streams[av_find_default_stream_index(avformat)];
}
if (avstream) {
fps = (double(avstream->avg_frame_rate.num) / double(avstream->avg_frame_rate.den));
codec_id = avstream->codecpar->codec_id;
duration = avstream->duration;
_vid.v_width = avstream->codecpar->width;
_vid.v_height = avstream->codecpar->height;
_vid.lastframe = duration / fps;
_vid.lastframe = avstream->nb_frames;
}
avcodec = avcodec_find_decoder(avstream->codecpar->codec_id);
avparser = av_parser_init(avcodec->id);
avcontext = avcodec_alloc_context3(avcodec);
avcontext->flags |= AVFMT_FLAG_NONBLOCK;
avcontext->flags |= AVFMT_FLAG_FLUSH_PACKETS;
avcontext->flags |= AVFMT_FLAG_DISCARD_CORRUPT;
avcontext->flags |= AVFMT_FLAG_NOBUFFER;
ret = avcodec_parameters_to_context(avcontext, avstream->codecpar);
ret = avcodec_open2(avcontext, avcodec, nullptr);
// Determine required buffer size and allocate buffer
auto numBytes = av_image_get_buffer_size(
AV_PIX_FMT_BGRA
, avcontext->width
, avcontext->height
, 1);
if (avinbuf)
av_free(avinbuf);
avinbuf = nullptr;
avinbuf = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
ret = av_image_fill_arrays(
avrgb->data
, avrgb->linesize
, avinbuf
, AV_PIX_FMT_BGRA
, avcontext->width
, avcontext->height
, 1);
sws_ctx = sws_getContext(
avcontext->width
, avcontext->height
, avcontext->pix_fmt
, avcontext->width
, avcontext->height
, AV_PIX_FMT_BGRA
, SWS_BILINEAR
, nullptr
, nullptr
, nullptr
);
}
int err = (sws_ctx && avcontext && avformat) ? 0 : 1;
// ...
}getting the frame :
uint8_t * video::app::get_frame(uint32_t frame) {
if (!avcontext)
return nullptr;
if (!avformat)
return nullptr;
if (!avpicture)
return nullptr;
if (!avfile)
return nullptr;
try {
int ret = 0;
if (avpicture->data)
av_frame_unref(avpicture);
while (true) {
if ((ret = av_read_frame(avformat, &avpkt)) < 0)
break;
if (avpkt.stream_index == avstream->index) {
ret = avcodec_send_packet(avcontext, &avpkt);
if (ret < 0)
break;
while (ret >= 0) {
ret = avcodec_receive_frame(avcontext, avpicture);
if (ret == AVERROR_EOF) {
return nullptr;
} else if (ret == -11) {
avpkt.data = nullptr;
avpkt.size = 0;
break;
} else if (ret < 0) {
return nullptr;
}
if (ret == AVERROR(EAGAIN)) {
avpkt.data = nullptr;
avpkt.size = 0;
break;
}
if (ret >= 0) {
int linesize[AV_NUM_DATA_POINTERS] = {
avpicture->linesize[0]
, avpicture->linesize[1]
, avpicture->linesize[2]
, avpicture->linesize[3]
, avpicture->linesize[4]
, avpicture->linesize[5]
, avpicture->linesize[6]
, avpicture->linesize[7]
};
uint8_t * data[AV_NUM_DATA_POINTERS] = {
avpicture->data[0]
, avpicture->data[1]
, avpicture->data[2]
, avpicture->data[3]
, avpicture->data[4]
, avpicture->data[5]
, avpicture->data[6]
, avpicture->data[7]
};
{
// flip the frame, never ever touch this thing again!
// If the planes in the image are unequal size(e.g.YUV420) you need to adapt the height.
auto h = avcontext->height;
for (int i = 0; i < 4; i++) {
if (i)
data[i] += linesize[i] * ((h >> 1) - 1);
else
data[i] += linesize[i] * (h - 1);
linesize[i] = -linesize[i];
}
}
ret = sws_scale(
sws_ctx
, (uint8_t const * const *)data
, linesize
, 0
, avcontext->height
, avrgb->data
, avrgb->linesize);
av_packet_unref(&avpkt);
currPts = avpkt.dts;
currPts *= av_q2d(avstream->time_base);
usleep(1000000 * (currPts - prevPts));
prevPts = currPts;
return avrgb->data[0];
}
}
}
av_packet_unref(&avpkt);
}
} catch (...) {
}
return nullptr;
} -
What is video timescale, timebase, or timestamp in ffmpeg ? [on hold]
11 avril 2017, par Please HelpThere does not seem to be any explanation online as to what these are. People talk about them a lot. I just want to know what they are and why they are significant. Using -video_track_timescale, how would I determine a number for it ? Is it random ? Should it be 0 ?
-
FFMPEG : AV out of sync when writing a part of a video to a new file
30 mars 2017, par IT_LaymanI’m developing a data preprocessing program for a computer vision project using FFMPEG and Face detection API. In this program, I need to extract the shots that contain human faces from a given input video file and output them into a new file. But when I played the output video file generated by that program, the video and audio track was out of sync. I think a possible reason is that the timestamp of video frame or audio frame is set incorrectly, but I can’t fix it by myself as I’m not very familiar with FFMPEG library, Please help me solving this out-of-sync issue.
To simplify the code shown below, I have removed all face detection code and use an empty function called
faceDetect
to represent it instead.// ffmpegAPI.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
extern "C" {
#include <libavutil></libavutil>opt.h>
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>avutil.h>
#include <libavutil></libavutil>channel_layout.h>
#include <libavutil></libavutil>common.h>
#include <libavutil></libavutil>imgutils.h>
#include <libavutil></libavutil>mathematics.h>
#include <libavutil></libavutil>samplefmt.h>
#include <libavutil></libavutil>pixdesc.h>
#include <libswscale></libswscale>swscale.h>
}
bool faceDetect(AVFrame *frame)
{
/*...*/
return true;
}
int main(int argc, char **argv)
{
int64_t videoPts = 0, audioPts = 0;
int samples_count = 0;
AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
AVOutputFormat *ofmt = NULL;
AVPacket pkt;
AVFrame *frame = NULL;
int videoindex = -1; int audioindex = -1;
double videoTime = DBL_MAX;
const char *in_filename, *out_filename;
int ret, i;
in_filename = "C:\\input.flv";//Input file name
out_filename = "C:\\output.avi";//Output file name
av_register_all();
//Open input file
if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
fprintf(stderr, "Could not open input file '%s'", in_filename);
goto end;
}
//Find input streams
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
fprintf(stderr, "Failed to retrieve input stream information");
goto end;
}
//Retrive AV stream information
for (i = 0; i < ifmt_ctx->nb_streams; i++)
{
AVStream *stream;
AVCodecContext *codec_ctx;
stream = ifmt_ctx->streams[i];//Get current stream
codec_ctx = stream->codec;//Get current stream codec
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoindex = i;//video stream index
}
else if (codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioindex = i;//audio stream index
}
if (videoindex == -1)//no video stream is found
{
printf("can't find video stream\n");
goto end;
}
}
av_dump_format(ifmt_ctx, 0, in_filename, 0);
//Configure output
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx) {
fprintf(stderr, "Could not create output context\n");
ret = AVERROR_UNKNOWN;
goto end;
}
ofmt = ofmt_ctx->oformat;
//Configure output streams
for (i = 0; i < ifmt_ctx->nb_streams; i++) {//Traversal input streams
AVStream *in_stream = ifmt_ctx->streams[i];//Get current stream
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);//Create a corresponding output stream
if (!out_stream) {
fprintf(stderr, "Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
goto end;
}
//Copy codec from current input stream to corresponding output stream
ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
if (ret < 0) {
fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
goto end;
}
if (i == videoindex)//Video stream
{
if (out_stream->codec->codec_id == AV_CODEC_ID_H264)
{
out_stream->codec->me_range = 16;
out_stream->codec->max_qdiff = 4;
out_stream->codec->qmin = 10;
out_stream->codec->qmax = 51;
out_stream->codec->qcompress = 1;
}
}
AVCodecContext *codec_ctx = out_stream->codec;
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
|| codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
//Find codec encoder
AVCodec *encoder = avcodec_find_encoder(codec_ctx->codec_id);
if (!encoder) {
av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
ret = AVERROR_INVALIDDATA;
goto end;
}
//Open encoder
ret = avcodec_open2(codec_ctx, encoder, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
goto end;
}
out_stream->codec->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
//Open the decoder for input stream
codec_ctx = in_stream->codec;
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
|| codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
ret = avcodec_open2(codec_ctx,
avcodec_find_decoder(codec_ctx->codec_id), NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
}
}
}
av_dump_format(ofmt_ctx, 0, out_filename, 1);
//Open output file for writing
if (!(ofmt->flags & AVFMT_NOFILE)) {
ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0) {
fprintf(stderr, "Could not open output file '%s'", out_filename);
goto end;
}
}
//Write video header
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0) {
fprintf(stderr, "Error occurred when opening output file\n");
goto end;
}
//Write frames in a loop
while (1) {
AVStream *in_stream, *out_stream;
//Read one frame from the input file
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];//Get current input stream
out_stream = ofmt_ctx->streams[pkt.stream_index];//Get current output stream
if (pkt.stream_index == videoindex)//video frame
{
int got_frame;
frame = av_frame_alloc();
if (!frame) {
ret = AVERROR(ENOMEM);
break;
}
//Readjust packet timestamp for decoding
av_packet_rescale_ts(&pkt,
in_stream->time_base,
in_stream->codec->time_base);
//Decode video frame
int len = avcodec_decode_video2(in_stream->codec, frame, &got_frame, &pkt);
if (len < 0)
{
av_frame_free(&frame);
av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
break;
}
if (got_frame)//Got a decoded video frame
{
int64_t pts = av_frame_get_best_effort_timestamp(frame);
//determine if the frame image contains human face
bool result = faceDetect(frame);
if (result) //face contained
{
videoTime = pts* av_q2d(out_stream->time_base);
frame->pts = videoPts++;//Set pts of video frame
AVPacket enc_pkt;
av_log(NULL, AV_LOG_INFO, "Encoding video frame\n");
//Create packet for encoding
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
//Encoding frame
ret = avcodec_encode_video2(out_stream->codec, &enc_pkt,
frame, &got_frame);
av_frame_free(&frame);
if (!(got_frame))
ret = 0;
/* Configure encoding properties */
enc_pkt.stream_index = videoindex;
av_packet_rescale_ts(&enc_pkt,
out_stream->codec->time_base,
out_stream->time_base);
av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
/* Write encoded frame */
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
else //no face contained
{
//Set the videoTime as maximum double value,
//making the corresponding audio frame not been processed
if (videoTime < DBL_MAX)
videoTime = DBL_MAX;
}
}
else
{
av_frame_free(&frame);
}
}
else//Audio frame
{
//Get current frame time
double audioTime = pkt.pts * av_q2d(in_stream->time_base);
if (audioTime >= videoTime)
{//The current frame should be written into output file
int got_frame;
frame = av_frame_alloc();
if (!frame) {
ret = AVERROR(ENOMEM);
break;
}
//Readjust packet timestamp for decoding
av_packet_rescale_ts(&pkt,
in_stream->time_base,
in_stream->codec->time_base);
//Decode audio frame
int len = avcodec_decode_audio4(in_stream->codec, frame, &got_frame, &pkt);
if (len < 0)
{
av_frame_free(&frame);
av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
break;
}
if (got_frame)//Got a decoded audio frame
{
//Set pts of audio frame
frame->pts = audioPts;
audioPts += frame->nb_samples;
AVPacket enc_pkt;
av_log(NULL, AV_LOG_INFO, "Encoding audio frame");
//Create packet for encoding
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
//Encode audio frame
ret = avcodec_encode_audio2(out_stream->codec, &enc_pkt,
frame, &got_frame);
av_frame_free(&frame);
if (!(got_frame))
ret = 0;
/* Configure encoding properties */
enc_pkt.stream_index = audioindex;
av_packet_rescale_ts(&enc_pkt,
out_stream->codec->time_base,
out_stream->time_base);
av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
/* Write encoded frame */
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
else //Shouldn't be written
{
av_frame_free(&frame);
}
}
}
av_packet_unref(&pkt);
}
//Write video trailer
av_write_trailer(ofmt_ctx);
end://Clean up
av_log(NULL, AV_LOG_INFO, "Clean up\n");
av_frame_free(&frame);
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
avcodec_close(ifmt_ctx->streams[i]->codec);
if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && ofmt_ctx->streams[i]->codec)
avcodec_close(ofmt_ctx->streams[i]->codec);
}
avformat_close_input(&ifmt_ctx);
/* Close output file */
if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
avio_closep(&ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
if (ret < 0 && ret != AVERROR_EOF) {
char buf[256];
av_strerror(ret, buf, sizeof(buf));
av_log(NULL, AV_LOG_ERROR, "Error occurred:%s\n", buf);
system("Pause");
return 1;
}
//Program end
printf("The End.\n");
system("Pause");
return 0;
}
</iostream>