Nilesh Bansal
2004-Nov-20 14:16 UTC
[Theora] ffmpeg2theora start and end time support - patch
After posting my previous mail, I realized that i should instead post a patch against HEAD. This is because v0.12 was released while I was also changing ffmpeg2theora.c from v0.11. So please use this patch instead and apply it to version at http://svn.xiph.org/trunk/ffmpeg2theora Nilesh Bansal http://www.cse.iitb.ac.in/nilesh/ -------------- next part -------------- Index: ffmpeg2theora.c ==================================================================--- ffmpeg2theora.c (revision 8237) +++ ffmpeg2theora.c (working copy) @@ -76,6 +76,9 @@ int video_y; int frame_x_offset; int frame_y_offset; + + int start_time; /* In seconds */ + int end_time; /* In seconds */ } *ff2theora; @@ -128,7 +131,8 @@ this->frame_bottomBand=0; this->frame_leftBand=0; this->frame_rightBand=0; - + this->start_time=0; + this->end_time=0; /* ZERO denotes no end time set */ } return this; } @@ -142,7 +146,7 @@ AVCodec *acodec = NULL; AVCodec *vcodec = NULL; float frame_aspect; - + int frame_number=0; double fps = 0.0; for (i = 0; i < this->context->nb_streams; i++){ @@ -426,9 +430,23 @@ info.vorbis_quality = this->audio_quality; info.vorbis_bitrate = this->audio_bitrate; theoraframes_init (&info); - + /*seek to start time*/ + av_seek_frame( this->context, -1, (int64_t)AV_TIME_BASE*this->start_time, 1); + /*check for end time and caclulate number of frames to encode*/ + int no_frames = fps*(this->end_time - this->start_time); + if(this->end_time > 0 && no_frames < 0){ + printf("Error -- end time < start time\n"); + exit(1); + } + /* main decoding loop */ - do{ + do{ + if(no_frames > 0){ + if(frame_number > no_frames){ + printf("\nTime to break\n"); + break; + } + } ret = av_read_frame(this->context,&pkt); if(ret<0){ e_o_s=1; @@ -449,6 +467,9 @@ //FIXME: move image resize/deinterlace/colorformat transformation // into seperate function if(got_picture){ + //For audio only files command line option"-e" will not work + //as we donot increment frame_number in audio section. + frame_number++; //FIXME: better colorformat transformation to YUV420P /* might have to cast other progressive formats here */ //if(venc->pix_fmt != PIX_FMT_YUV420P){ @@ -644,6 +665,8 @@ "\t --audiobitrate,-A\t[45 to 2000] encoding bitrate for audio\n" "\t --samplerate,-H\tset output samplerate in Hz\n" "\t --nosound\t\tdisable the sound from input\n" + "\t --endtime,-e\t\tend encoding at this time (in sec)\n" + "\t --starttime,-s\t\tstart encoding at this time (in sec)\n" "\t --v2v-preset,-p\tencode file with v2v preset, \n" "\t\t\t\t right now there is preview and pro,\n" "\t\t\t\t '"PACKAGE" -p info' for more informations\n" @@ -704,7 +727,7 @@ av_register_all (); int c,long_option_index; - const char *optstring = "o:f:x:y:v:V:a:A:d:H:c:p:N:D:h::"; + const char *optstring = "o:f:x:y:v:V:a:s:e:A:d:H:c:p:N:D:h::"; struct option options [] = { {"output",required_argument,NULL,'o'}, {"format",required_argument,NULL,'f'}, @@ -726,6 +749,8 @@ {"cropright",required_argument,&cropright_flag,1}, {"cropleft",required_argument,&cropleft_flag,1}, {"inputfps",required_argument,&inputfps_flag,1}, + {"starttime",required_argument,NULL,'s'}, + {"endtime",required_argument,NULL,'e'}, {"artist",required_argument,&metadata_flag,10}, {"title",required_argument,&metadata_flag,11}, @@ -807,6 +832,12 @@ metadata_flag=0; } break; + case 'e': + convert->end_time = atoi(optarg); + break; + case 's': + convert->start_time = atoi(optarg); + break; case 'o': sprintf(outputfile_name,optarg); outputfile_set=1;