Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
Where ffmpeg flags like CODEC_FLAG2_BPYRAMID are located ?
27 novembre 2012, par theateistI'm developing on using VS2010 with ffmpeg and tried the code from here. But VS says that it cannot find
CODEC_FLAG2_BPYRAMID+CODEC_FLAG2_MIXED_REFS+CODEC_FLAG2_WPRED+CODEC_FLAG2_8X8DCT+CODEC_FLAG2_FASTPSKIP; // flags2=+bpyramid+mixed_refs+wpred+dct8x8+fastpskip
X264_PART_I8X8+X264_PART_I4X4+X264_PART_P8X8+X264_PART_B8X8
avCodecContext.crf
Where are they located?
-
fps porblem when saving video in mp4 container
27 novembre 2012, par theateistWhen I decode frames from avi file and then decode them in x264 and save to mp4 file, the fps of the output file is always 12,800. Therefore the file is played very fast. But, when I save the encoded in h264 frames in avi format and not mp4, so the fps is as I wanted - 25.
What could be the problem? This is very urgent to me. I'm stuck with this and cannot continue with my project!
Here the code I wrote in VS2010:
#include "stdafx.h" #include "inttypes.h" extern "C" { #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libavutil/avutil.h" #include
swscale.h> #include opt.h> #include swscale.h> #include imgutils.h> } #include using namespace std; int main(int argc, char* argv[]) { const char* inFileName = "C:\\000227_C1_GAME.avi"; const char* outFileName = "c:\\test.avi"; const char* outFileType = "avi"; av_register_all(); AVFormatContext* inContainer = NULL; if(avformat_open_input(&inContainer, inFileName, NULL, NULL) < 0) exit(1); if(avformat_find_stream_info(inContainer, NULL) < 0) exit(1); // Find video stream int videoStreamIndex = -1; for (unsigned int i = 0; i < inContainer->nb_streams; ++i) { if (inContainer->streams[i] && inContainer->streams[i]->codec && inContainer->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) exit(1); AVFormatContext* outContainer = NULL; if(avformat_alloc_output_context2(&outContainer, NULL, outFileType, outFileName) < 0) exit(1); // ---------------------------- // Decoder // ---------------------------- AVStream const *const inStream = inContainer->streams[videoStreamIndex]; AVCodec *const decoder = avcodec_find_decoder(inStream->codec->codec_id); if(!decoder) exit(1); if(avcodec_open2(inStream->codec, decoder, NULL) < 0) exit(1); // ---------------------------- // Encoder // ---------------------------- AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264); if(!encoder) exit(1); AVStream *outStream = avformat_new_stream(outContainer, encoder); if(!outStream) exit(1); avcodec_get_context_defaults3(outStream->codec, encoder); // Construct encoder if(outContainer->oformat->flags & AVFMT_GLOBALHEADER) outStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; outStream->codec->coder_type = AVMEDIA_TYPE_VIDEO; outStream->codec->pix_fmt = AV_PIX_FMT_YUV420P; outStream->codec->width = inStream->codec->width; outStream->codec->height = inStream->codec->height; outStream->codec->codec_id = encoder->id; outStream->codec->bit_rate = 500000; //outStream->codec->rc_min_rate = 600000; //outStream->codec->rc_max_rate = 800000; outStream->codec->time_base.den = 25; outStream->codec->time_base.num = 1; outStream->codec->gop_size = 250; // Keyframe interval(=GOP length). Determines maximum distance distance between I-frames outStream->codec->keyint_min = 25; // minimum GOP size outStream->codec->max_b_frames = 3;//16; // maximum number of B-frames between non-B-frames outStream->codec->b_frame_strategy = 1; // decides the best number of B-frames to use. Default mode in x264. outStream->codec->scenechange_threshold = 40; outStream->codec->refs = 6; // abillity to reference frames other than the one immediately prior to the current frame. specify how many references can be used. outStream->codec->qmin = 0;//10; outStream->codec->qmax = 69;//51; outStream->codec->qcompress = 0.6; outStream->codec->max_qdiff = 4; outStream->codec->i_quant_factor = 1.4;//0.71; outStream->codec->refs=1;//3; outStream->codec->chromaoffset = -2; outStream->codec->thread_count = 1; outStream->codec->trellis = 1; outStream->codec->me_range = 16; outStream->codec->me_method = ME_HEX; //hex outStream->codec->flags2 |= CODEC_FLAG2_FAST; outStream->codec->coder_type = 1; if(outStream->codec->codec_id == AV_CODEC_ID_H264) { av_opt_set(outStream->codec->priv_data, "preset", "slow", 0); } // Open encoder if(avcodec_open2(outStream->codec, encoder, NULL) < 0) exit(1); // Open output container if(avio_open(&outContainer->pb, outFileName, AVIO_FLAG_WRITE) < 0) exit(1); //close_o AVFrame *decodedFrame = avcodec_alloc_frame(); if(!decodedFrame) exit(1); AVFrame *encodeFrame = avcodec_alloc_frame(); if(!encodeFrame) exit(1); encodeFrame->format = outStream->codec->pix_fmt; encodeFrame->width = outStream->codec->width; encodeFrame->height = outStream->codec->height; if(av_image_alloc(encodeFrame->data, encodeFrame->linesize, outStream->codec->width, outStream->codec->height, outStream->codec->pix_fmt, 1) < 0) exit(1); av_dump_format(inContainer, 0, inFileName,0); //Write header to ouput container avformat_write_header(outContainer, NULL); AVPacket decodePacket, encodedPacket; int got_frame, len; while(av_read_frame(inContainer, &decodePacket)>=0) { if (decodePacket.stream_index == videoStreamIndex) { len = avcodec_decode_video2(inStream->codec, decodedFrame, &got_frame, &decodePacket); if(len < 0) exit(1); if(got_frame) { av_init_packet(&encodedPacket); encodedPacket.data = NULL; encodedPacket.size = 0; if(avcodec_encode_video2(outStream->codec, &encodedPacket, decodedFrame, &got_frame) < 0) exit(1); if(got_frame) { if (outStream->codec->coded_frame->key_frame) encodedPacket.flags |= AV_PKT_FLAG_KEY; encodedPacket.stream_index = outStream->index; if(av_interleaved_write_frame(outContainer, &encodedPacket) < 0) exit(1); av_free_packet(&encodedPacket); } } } av_free_packet(&decodePacket); } av_write_trailer(outContainer); avio_close(outContainer->pb); avcodec_free_frame(&encodeFrame); avcodec_free_frame(&decodedFrame); avformat_free_context(outContainer); av_close_input_file(inContainer); return 0; } -
av_write_frame fails when encoding a larger audio file to .mpg
27 novembre 2012, par TheSHEEEPI am encoding live rendered video data and an existing .wav file into an mpg-file.
To do that I first write all audio frames, and then the video frames as they come in from the render engine. For smaller .wav files (< 25 seconds), everything works perfectly fine. But as soon as I use a longer .wav file, av_write_frame (when writing the audio frame) just returns -1 after having written some 100 frames. It is never the same frame at which it fails, also it is never the last frame. All test files can be played perfectly with any player I tested.
I am following the muxing example (more or less).
Here is my function that writes an audio frame:
void write_audio_frame( Cffmpeg_dll * ptr, AVFormatContext *oc, AVStream *st, int16_t sample_val ) { AVCodecContext *c; AVPacket pkt = { 0 }; // data and size must be 0; AVFrame *frame = avcodec_alloc_frame(); int got_packet; av_init_packet(&pkt); c = st->codec; get_audio_frame(ptr, ptr->samples, ptr->audio_input_frame_size, c->channels); frame->nb_samples = ptr->audio_input_frame_size; int result = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, (uint8_t *) ptr->samples, ptr->audio_input_frame_size * av_get_bytes_per_sample(c->sample_fmt) * c->channels, 0); if (result != 0) { av_log(c, AV_LOG_ERROR, "Error filling audio frame. Code: %i\n", result); exit(1); } result = avcodec_encode_audio2(c, &pkt, frame, &got_packet); if (result != 0) { av_log(c, AV_LOG_ERROR, "Error encoding audio. Code: %i\n", result); exit(1); } if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE) pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base); pkt.flags |= AV_PKT_FLAG_KEY; pkt.stream_index= st->index; av_log(c, AV_LOG_ERROR, "Got? %i Pts: %i Dts: %i Flags: %i Side Elems: %i Size: %i\n", got_packet, pkt.pts, pkt.dts, pkt.flags, pkt.side_data_elems, pkt.size); /* write the compressed frame in the media file */ result = av_write_frame(oc, &pkt); if (result != 0) { av_log(c, AV_LOG_ERROR, "Error while writing audio frame. Result: %i\n", result); exit(1); } }
So "Error while writing audio frame. Result: -1" is what I always get after some frames.
And here is my get_audio_frame function:
void get_audio_frame( Cffmpeg_dll* ptr, int16_t* samples, int frame_size, int nb_channels, int16_t sample_val ) { fread( samples, sizeof( int16_t ), frame_size * nb_channels, ptr->fp_sound_input ); };
And finally, this is the loop in which I write all audio frames (don't worry about the .wav header, I skipped it before that loop):
while (!feof(ptr->fp_sound_input)) { write_audio_frame( ptr, ptr->oc, ptr->audio_st, -1 ); }
As you can see, I'm outputting almost everything in the packet and check for any possible error. Other than av_write_frame failing after some time when I am encoding a longer audio file, everything seems perfectly fine. All the packet values I am tracking are 100% the same for all frames (except the data pointer, obviously). Also, as stated, the same procedure works flawlessly for shorter fp_sound_input files. avcodec_encode_audio2() and avcodec_fill_audio_frame() also never fail.
The codecs I use for encoding are CODEC_ID_MPEG2VIDEO (video) and CODEC_ID_MP2 (audio). The .wav files are saved in PCM 16 LE (all use the exact same encoding).
What could be wrong here?
-
convert raw video .y4m to mpg video with certain GOP
27 novembre 2012, par Abeer AwadI want to compress a raw video .y4m to mpg, and I want then to extract the frames from the mpg video, I need the GOP of the compression to be
:IBBPBBPBBPBBPBBIBBP....15:2
I used this command:
ffmpeg -i video.ym4 -vcodec libx264 -sameq -y -r 30 output.avi 2>list.txt ffmpeg -i output.avi -vcodec libx264 -y -sameq -vf showinfo -y -f image2 image%3d.jpeg -r 30 2>list1.txt
The output contains only 2 I frames,
100 P
and198 B
frames, it is not15:2 GOP
, what to do?I need one
I-frame
every 15 frames, and the pattern to bIBBPBBP..
Sorry, Im new to
ffmpeg
, please help me, this is the input to my project, it is the important step to me. -
Creating a movie with Bullet Physics
27 novembre 2012, par SaraI am trying to make a movie from my bullet demo. To do that, I want to store an image every timestep, and create a movie using ffmpeg.
I need the simulation to advance only one step at a time. For that, I am doing
m_dynamicsWorld->stepSimulation(0.005,0);
And after that, I store the scenario (I later create an image of the scenario). At the end of the animation, I am creating a movie using as timestep 200 fps (1/0.005). This should give me the real time animation, but instead the animation is a little bit slow.
Any ideas?