>
> Sorry code not attached in previous mail
>
> Hi
>
> If I try converting avi using command line it works OK. If I use library
> calls I get following error.
>
> My conversion code is attached.
>
> 2010-10-21 18:43:23.573 iFrameExtractor[41483:207] video duration: 3.320454
> 2010-10-21 18:43:23.574 iFrameExtractor[41483:207] video size: 640 x 480
> Input #1, avi, from '/Users/johngladman/Library/Application Support/iPhone
> Simulator/4.0/Applications/0FC01E72-9F4B-4E00-AAF9-DF3113C4C2CB/iFrameExtractor.app/sample.avi':
> Duration: 00:00:00.00, start: 0.000000, bitrate: -2147483 kb/s
> Stream #1.0: Video: mjpeg, yuvj420p, 320x390 [PAR 1:1 DAR 32:39], 6 tbr, 6
> tbn, 6 tbc
> Stream #1.1: Audio: pcm_s16le, 44100 Hz, 1 channels, s16, 705 kb/s
> [mpeg4 @ 0x6811c00]removing common factors from framerate
> [mp4 @ 0x6825400]track 1: could not find tag, codec not currently supported
> in container
> Could not write header for output file!
> /Users/johngladman/iFrameExtractor/Classes/CopyVideo.m:180
> Output #0, mp4, to '/Users/johngladman/Library/Application Support/iPhone
> Simulator/4.0/Applications/0FC01E72-9F4B-4E00-AAF9-DF3113C4C2CB/Documents/sample.mp4':
> Stream #0.0: Video: mpeg4, yuv420p, 320x390 [PAR 1:1 DAR 32:39], q=2-31,
> 200 kb/s, 3600 tbn, 3600 tbc
> Stream #0.1: Invalid Codec type -1
>
int Convert(const char *inputVideoPath, const char *outputPath){
if(inputVideoPath == NULL || outputPath == NULL){
fprintf(stderr, "Video path or image path aren't set!\n");
return -1;
}
av_register_all();
avcodec_register_all();
AVFormatContext *ic;
AVInputFormat *ifmt;
ifmt = av_find_input_format("avi");
if(!ifmt){
fprintf(stderr, "Nu gaseste demuxer pt mov\n");
return -1;
}
if(av_open_input_file(&ic, inputVideoPath, ifmt, 0, NULL) != 0){
fprintf(stderr, "Nu poate deschide fisierul de intrare! %s\n",
inputVideoPath);
return -1;
}
if(av_find_stream_info(ic) < 0){
fprintf(stderr, "Nu s-a gasit un stream pt fisierul de
intrare\n");
return -1;
}
ic->flags |= AVFMT_FLAG_NONBLOCK;
ic->loop_input = 0;
AVFormatContext *oc;
AVOutputFormat *ofmt;
// const char* fileName = "record.mp4";
//ofmt = av_guess_format(NULL, fileName, NULL);
ofmt = av_guess_format("mp4", NULL, NULL);
if(!ofmt){
fprintf(stderr, "Nu s-a gasit muxer pt mov (output)\n");
return -1;
}
oc = avformat_alloc_context();
if(!oc){
fprintf(stderr, "nu s-a putut aloca contextul de iesire\n");
return -1;
}
oc->oformat = ofmt;
//const char* filename = "record.mp4"
strcpy(oc->filename, outputPath);
int video_index = -1, audio_index = -1;
AVStream *video_st, *audio_st;
AVCodecContext *video_enc;
for(int i=0; i<ic->nb_streams; i++){
if(ic->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO){
video_index = i;
ic->streams[i]->discard = AVDISCARD_NONE;
video_st = av_new_stream(oc, oc->nb_streams);
if (!video_st) {
fprintf(stderr, "Could not alloc stream\n");
return -1;
}
avcodec_get_context_defaults2(video_st->codec,
CODEC_TYPE_VIDEO);
/* for(int i=0; i<3; i++){
video_st->auxMatrix[i][0] =
ic->streams[video_index]->auxMatrix[i][0];
video_st->auxMatrix[i][1] =
ic->streams[video_index]->auxMatrix[i][1];
}*/
video_enc = video_st->codec;
video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
video_enc->width =
ic->streams[video_index]->codec->width;
video_enc->height =
ic->streams[video_index]->codec->height;
video_enc->pix_fmt = PIX_FMT_YUV420P;
video_enc->flags |= CODEC_FLAG_QSCALE;
video_enc->global_quality = video_st->quality = 0;
if(av_q2d(video_enc->time_base)*video_enc->ticks_per_frame >
av_q2d(video_st->time_base) && av_q2d(video_st->time_base) < 1.0/1000){
video_enc->time_base =
ic->streams[video_index]->codec->time_base;
video_enc->time_base.num *=
ic->streams[video_index]->codec->ticks_per_frame;
}else
video_enc->time_base = video_st->time_base;
video_enc->time_base.num = 25;
// video_enc->time_base.den =
ic->streams[video_index]->time_base.den;
// video_enc->time_base.num =
ic->streams[video_index]->time_base.num;
video_enc->sample_aspect_ratio =
av_d2q(video_enc->height/video_enc->width, 255);
video_st->sample_aspect_ratio =
video_enc->sample_aspect_ratio;
{
video_enc->thread_count=1;
video_enc->rc_override_count=0;
video_enc->rc_initial_buffer_occupancy =
video_enc->rc_buffer_size*3/4; //0
video_enc->me_threshold = 0;
video_enc->intra_dc_precision = 0;
}
oc->streams[video_index]->codec->bits_per_raw_sample=
ic->streams[video_index]->codec->bits_per_raw_sample;
oc->streams[video_index]->codec->chroma_sample_location
= ic->streams[video_index]->codec->chroma_sample_location;
}
else if(ic->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO){
//////////
audio_index = i;
audio_st = av_new_stream(oc, ic->streams[audio_index]);
audio_st->codec->codec_id == CODEC_ID_AAC;
audio_st->stream_copy = 1;
ic->streams[i]->discard = AVDISCARD_NONE;
audio_st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
audio_st->codec->channel_layout =
ic->streams[i]->codec->channel_layout;
audio_st->codec->sample_rate =
ic->streams[i]->codec->sample_rate;
audio_st->codec->channels =
ic->streams[i]->codec->channels;
audio_st->codec->frame_size =
ic->streams[i]->codec->frame_size;
audio_st->codec->block_align=
ic->streams[i]->codec->block_align;
}
else{
ic->streams[i]->discard = AVDISCARD_ALL;
}
}
if(video_index<0){
fprintf(stderr, "Can't find any video stream! %s:%d\n",
__FILE__, __LINE__);
return -1;
}
if(audio_index<0){
fprintf(stderr, "Can't find any audio stream! %s:%d\n",
__FILE__, __LINE__);
goto fail;
}
dump_format(ic, 1, inputVideoPath, 0);
if(url_fopen(&oc->pb, outputPath, URL_WRONLY)<0){
fprintf(stderr, "Nu s-a putut deschide fisierul de iesire
%s:%d\n", __FILE__, __LINE__);
goto fail;
}
uint8_t *bit_buffer = NULL;
int bit_buffer_size =1024*256;
int size = video_enc->width*video_enc->height;
bit_buffer_size = FFMAX(bit_buffer_size, 6*size+200);
bit_buffer = av_malloc(bit_buffer_size);
if(!bit_buffer){
fprintf(stderr, "Could not allocate buffer\n");
}
enum CodecID codec_id;
AVCodec *codec;
codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL,
CODEC_TYPE_VIDEO);
codec = avcodec_find_encoder(codec_id);
video_enc->codec_id = codec_id;
if(!codec){
fprintf(stderr, "Unsupported coder! %s:%d", __FILE__, __LINE__);
goto fail;
}
if (avcodec_open(video_enc, codec) < 0) {
fprintf(stderr, "Could not open codec for encoding! %s:%d\n",
__FILE__, __LINE__);
goto fail;
}
AVCodec *pCodec;
pCodec=avcodec_find_decoder(ic->streams[video_index]->codec->codec_id);
if(!pCodec) {
fprintf(stderr, "Unsupported decoder! %s:%d\n", __FILE__,
__LINE__);
avcodec_close(video_enc);
goto fail;
}
if (avcodec_open(ic->streams[video_index]->codec, pCodec) < 0) {
fprintf(stderr, "Could not open codec for decoding! %s:%d\n",
__FILE__, __LINE__);
avcodec_close(video_enc);
goto fail;
}
if(av_write_header(oc)<0){
fprintf(stderr, "Could not write header for output file!
%s:%d\n", __FILE__, __LINE__);
avcodec_close(ic->streams[video_index]->codec);
avcodec_close(video_enc);
goto fail;
}
int decode_done = 0, got_picture=0;
do {
AVPacket packet;
decode_done = av_read_frame(ic, &packet);
if(decode_done<0){
fprintf(stdin, "Decodare terminata");
break;
}
if(ic->streams[packet.stream_index]->codec->codec_type ==
CODEC_TYPE_VIDEO){
AVFrame picture;
avcodec_get_frame_defaults(&picture);
int ret =
avcodec_decode_video2(ic->streams[video_index]->codec, &picture, &got_picture,
&packet);
ic->streams[video_index]->quality= picture.quality;
if(ret<0){
av_free_packet(&packet);
fprintf(stderr, "Could not decode packet!
%s:%d\n", __FILE__, __LINE__);
break;
}
if(!got_picture){
fprintf(stderr, "Didn't get picture! %s:%d\n",
__FILE__, __LINE__);
av_free_packet(&packet);
av_free(&picture);
continue;
}
packet.size = 0;
picture.pict_type=0;
ret = avcodec_encode_video(video_enc, bit_buffer,
bit_buffer_size, &picture);
//av_free(picture);
if(ret<0){
fprintf(stderr, "Video encoding failed!
%s:%d\n", __FILE__, __LINE__);
continue;
}
if(ret>0){
AVPacket opacket;
av_init_packet(&opacket);
opacket.pts = opacket.dts = packet.pts;
opacket.stream_index = video_index;
opacket.data = bit_buffer;
opacket.size = ret;
if(video_enc->coded_frame &&
video_enc->coded_frame->key_frame)
opacket.flags |= PKT_FLAG_KEY;
ret = av_interleaved_write_frame(oc, &opacket);
if(ret<0){
fprintf(stderr, "Error writing frame!
%s:%d\n", __FILE__, __LINE__);
}
av_free_packet(&opacket);
}
}
else if(ic->streams[packet.stream_index]->codec->codec_type ==
CODEC_TYPE_AUDIO){
packet.stream_index = audio_index;
int ret = av_interleaved_write_frame(oc, &packet);
if(ret<0){
fprintf(stderr, "Error writing frame! %s:%d\n",
__FILE__, __LINE__);
}
}
else{
av_free_packet(&packet);
continue;
}
av_free_packet(&packet);
} while(!decode_done);
av_free(bit_buffer);
av_write_trailer(oc);
av_freep(&ic->streams[video_index]->codec->stats_in);
avcodec_close(ic->streams[video_index]->codec);
avcodec_close(video_enc);
dump_format(oc, 0, outputPath, 1);
for(int i = 0; i < oc->nb_streams; i++) {
av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
}
url_fclose(oc->pb);
av_free(oc);
av_close_input_file(ic);
return 0;
fail:
dump_format(oc, 0, outputPath, 1);
for(int i = 0; i < oc->nb_streams; i++) {
av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
}
url_fclose(oc->pb);
av_free(oc);
av_close_input_file(ic);
return -1;
}
@implementation CopyVideo
- (void) copyFromAvi:(NSString *) aviMoviePath toMP4:(NSString *) mp4MoviePath
title:(NSString *) title
{
Convert([aviMoviePath UTF8String], [mp4MoviePath UTF8String]);
}
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user