I am using ffmpeg for my project to convert a wmv file to an mpg file. The code is in c. I am saving the final output to a file Myvideo. When i open the file Myvideo after conversion using ffmpeg (ffmpeg -i Myvideo), i get the following output:
FFmpeg version SVN-r14396, Copyright (c) 2000-2008 Fabrice Bellard, et al.
configuration: --enable-shared --enable-libvorbis --enable-libmp3lame --enable-gpl --enable-libfaad --enable-libfaac --enable-encoder=x264
libavutil version: 49.7.0
libavcodec version: 51.60.0
libavformat version: 52.18.0
libavdevice version: 52.0.0
built on Aug 8 2008 18:11:50, gcc: 4.0.2 20051125 (Red Hat 4.0.2-8)
Input #0, mpegvideo, from 'Myvideo':
Duration: 00:00:00.00, bitrate: 104561 kb/s
Stream #0.0: Video: mpeg2video, yuv420p, 352x288 [PAR 1:1 DAR 11:9], 104857 kb/s, 30.00 tb(r)
I could find that all the properties are correct, but the duration is always 0 an deven the size of file is in a few kb while the size of my input file is around 4 Mb. I couldnt really get the issue here. Please find below my code. I will be grateful, if somebody could help me in resolving this issue.
The input file provided is a wmv file through an argument.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <libavutil/common.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
struct video {
AVFormatContext *pFormatCtx;
int i, videoStream;
AVCodec *pCodec;
AVCodecContext *pCodecCtx;
AVFrame *pFrame;
int numBytes;
uint8_t *buffer;
uint8_t *frame_buffer;
};
<code>
int main(int argc, char *argv[])
{
printf("Server started\n");
int errno;
int status,image_size=0,i=0;
struct video *video_frame=(struct video*)malloc(sizeof(struct video));
AVPacket packet;
int frameFinished;
//for storing the output
FILE *pFile;
const char szFilename[32]= "Myvideo";
//registering codecs
av_register_all();
if(av_open_input_file(&(video_frame->pFormatCtx), argv[1], NULL, 0, NULL)!=0)
{
fprintf(stderr, "Unable to open the video: %s\n", argv[1]);
free(video_frame);
goto FINISH;
}
if(av_find_stream_info(video_frame->pFormatCtx)<0)
{
fprintf(stderr, "Unable to find stream information: %s\n", argv[1]);
free(video_frame);
goto FINISH;
}
printf("dump:\n");
dump_format(video_frame->pFormatCtx, 0, argv[1], 0);
printf("streams: %d\n",video_frame->pFormatCtx->nb_streams);
printf("filename:%s\n",video_frame->pFormatCtx->filename);
printf("duration:%d\n",video_frame->pFormatCtx->duration);
printf("filesize:%d\n",video_frame->pFormatCtx->file_size);
printf("bitrate:%d\n",video_frame->pFormatCtx->bit_rate);
printf("muxrate:%d\n",video_frame->pFormatCtx->mux_rate);
printf("Packet size:%d\n",video_frame->pFormatCtx->packet_size);
//Find the first video stream
video_frame->videoStream=-1;
for(i=0;i<video_frame->pFormatCtx->nb_streams;i++)
{
if(video_frame->pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
{
//printf("codectype");
video_frame->videoStream=i;
break;
}
}
if(video_frame->videoStream==-1)
{
fprintf(stderr,"Couldnt find video stream: %s\n", argv[1]);
free(video_frame);
goto FINISH;
}
//Get a pointer to the video stream
//video_frame->pCodecCtx=&(video_frame->pFormatCtx)->streams[video_frame->videoStream]->codec;
video_frame->pCodecCtx=video_frame->pFormatCtx->streams[video_frame->videoStream]->codec;
//Find the decoder for the video stream
video_frame->pCodec=avcodec_find_decoder(video_frame->pCodecCtx->codec_id);
//video_frame->pCodec=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO);
if(video_frame->pCodec==NULL)
{
fprintf(stderr, "Unsupported Codec!:%s\n", argv[1]);
free(video_frame);
goto FINISH;
}
if(avcodec_open(video_frame->pCodecCtx,video_frame->pCodec)<0)
{
fprintf(stderr, "Couldn't open codec: %s\n", argv[1]);
free(video_frame);
goto FINISH;
}
video_frame->pFrame=avcodec_alloc_frame();
if(video_frame->pFrame==NULL)
{
free(video_frame);
goto FINISH;
}
printf("format:%d\n",video_frame->pCodecCtx->pix_fmt);
printf("width:%d\n",video_frame->pCodecCtx->width);
printf("height:%d\n",video_frame->pCodecCtx->height);
// Determine required buffer size and allocate buffer
video_frame->numBytes=avpicture_get_size(video_frame->pCodecCtx->pix_fmt, video_frame->pCodecCtx->width, video_frame->pCodecCtx->height);
//video_frame->numBytes=avpicture_get_size(PIX_FMT_YUV420P, 320, 240);
//printf("Before size");
printf("size: %d bytes\n",video_frame->numBytes);
//printf("After size");
video_frame->buffer=(uint8_t*)av_malloc(video_frame->numBytes*sizeof(uint8_t));
// Assign appropriate parts of buffer to image planes in pFrameRGB
// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
// of AVPicture
image_size=avpicture_fill((AVPicture *)video_frame->pFrame, video_frame->buffer, video_frame->pCodecCtx->pix_fmt, video_frame->pCodecCtx->width, video_frame->pCodecCtx->height);
//image_size=avpicture_fill((AVPicture *)video_frame->pFrame, video_frame->buffer, PIX_FMT_YUV420P, 320, 240);
printf("Image Size: %d\n", image_size);
int j=0;
while(av_read_frame(video_frame->pFormatCtx, &packet)>=0)
{
// Is this a packet from the video stream?
if(packet.stream_index==video_frame->videoStream)
{
// Decode video frame
int i=0;
i=avcodec_decode_video(video_frame->pCodecCtx, video_frame->pFrame, &frameFinished,packet.data,packet.size);
//printf("%d\n",i);
// Did we get a video frame?
if(frameFinished)
{
int height=288;
int width=352;
int frame_rate = 30;
size_t size;
struct video *target=malloc(sizeof(struct video));
AVPicture pic;
//av_register_all();
//printf("Finding Codec\n");
target->pCodec=avcodec_find_encoder(CODEC_ID_MPEG2VIDEO);
if(!target->pCodec)
{
fprintf(stderr, "No proper codec found\n");
free(target);
goto FINISH;
}
//printf("Allocating context\n");
target->pCodecCtx=avcodec_alloc_context();
//printf("Allocating frame\n");
target->pFrame=avcodec_alloc_frame();
if(target->pFrame==NULL)
{
fprintf(stderr,"Could not allocate pFrame\n");
avcodec_close(target->pCodecCtx);
free(target);
goto FINISH;
}
//printf("Assigning values\n");
target->pCodecCtx->bit_rate=48000;
target->pCodecCtx->width=width;
target->pCodecCtx->height=height;
//target->pCodecCtx->frame_rate=30;
//target->pCodecCtx->frame_rate_base=1;
target->pCodecCtx->gop_size=10;
//added
//target->pCodecCtx->qmin=0;
//target->pCodecCtx->qmax=0;
//target->pCodecCtx->max_qdiff=0;
target->pCodecCtx->max_b_frames=0; //h263 doesnt have any b-frames
target->pCodecCtx->codec_id = CODEC_ID_MPEG2VIDEO;
target->pCodecCtx->codec_type = CODEC_TYPE_VIDEO;
target->pCodecCtx->time_base.num = 1;
target->pCodecCtx->time_base.den = frame_rate;
target->pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
target->pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
//printf("before open avcodec\n");
if (avcodec_open(target->pCodecCtx, target->pCodec) < 0)
{
fprintf(stderr, "Could not open the codec while preparing to " \
"encode the video stream.\n");
goto FINISH;
}
target->numBytes=video_frame->numBytes;
//printf("targetnumBytes:%d\n",target->numBytes);
target->buffer=malloc(target->numBytes);
//if (errno == ENOMEM)
{
//fprintf(stderr, "An error occured while allocating memory for " \
"the buffer variable in video_prepare_encode.\n");
//goto DONE;
}
size = width * height;
target->frame_buffer = malloc((size * 3) / 2); // size for YUV 420
//if (errno == ENOMEM)
{
//fprintf(stderr, "An error occured while allocating memory for " \
"the frame_buffer variable in video_prepare_encode.\n");
//goto DONE;
}
target->pFrame->data[0] = target->frame_buffer;
target->pFrame->data[1] = target->pFrame->data[0] + size;
target->pFrame->data[2] = target->pFrame->data[1] + size / 4;
target->pFrame->linesize[0] = target->pCodecCtx->width;
target->pFrame->linesize[1] = target->pCodecCtx->width / 2;
target->pFrame->linesize[2] = target->pCodecCtx->width / 2;
avpicture_fill((AVPicture*)target->pFrame, target->frame_buffer,
PIX_FMT_YUV420P, width, height);
//printf("Converting Image\n");
img_convert((AVPicture*)target->pFrame, PIX_FMT_YUV420P, (AVPicture*)video_frame->pFrame,video_frame->pCodecCtx->pix_fmt, width, height);
int num=0;
num=avcodec_encode_video(target->pCodecCtx, target->buffer, target->numBytes, target->pFrame);
if(num<0)
{
fprintf(stderr,"Encoding error\n");
goto FINISH;
}
//saving to the file
pFile=fopen(szFilename, "wb");
if (!pFile) {
fprintf(stderr, "could not open %s\n", szFilename);
goto FINISH;
}
fwrite(target->buffer,1,num,pFile);
fclose(pFile);
free(target->frame_buffer);
free(target->buffer);
avcodec_close(target->pCodecCtx);
av_free(target->pCodecCtx);
av_free(target->pFrame);
j++;
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
printf("%d\n",j);
printf("Video Converted\n");
av_free(video_frame->buffer);
av_free(video_frame->pFrame);
avcodec_close(video_frame->pCodecCtx);
av_close_input_file(video_frame->pFormatCtx);
free(video_frame);
goto FINISH;
FINISH:
return 0;
}
Code: Select all