<div dir="ltr">Hi all,<br>I am trying to seek to particular locations(where subs/CC present) in audio stream in different formats of videos using av_seek_frame. I am getting result for just one video of MPEG2-TS format by using (ts = time_in_sec * INPUT_TIMEBASE.denominator). For all others they are just seeking to last PTS same as that in stream->duration. I searched for solutions, and got some mentioning  av_rescale_q(time_in_sec,AV_TIME_BASE_Q,INPUT_TIME_BASE) but that doesn't works. I have following questions 1) What method should I use to convert time in seconds to in time_base units for an audio stream. 2) For rest videos the seeked frames have PTS as same as duration. 3) Does function av_seek_frame has some drawbacks or formats to which it is not applicable or doesn't works well. What does that means? Here is code I am sharing with you.<br> <br><div style="text-align:left"> void process_frame_by_pts(uint16_t index,int64_t time_to_seek_ms)<br>{<br>  int64_t start_pts =(int64_t) (time_to_seek_ms/1000) * INPUT_TIMEBASE.den ;<br>  int64_t end_pts = ((time_to_seek_ms + 1500)/1000) * INPUT_TIMEBASE.den;<br>  int i = 0, count = 0,temp = 0,out_count = 0,in_count = 0;<br>  AVPacket pkt;<br>  int got_frame,ret;<br>  AVFrame *frame = av_frame_alloc();<br>  int size,len,buf_size;<br>  int err;<br>  char errstr[128];<br>  if(end_pts > fmt_ctx->streams[audio_stream_index]->duration){<br>    printf("Error: End PTS(%lu) greater then duration of stream(%lu)\n",end_pts,fmt_ctx->streams[audio_stream_index]->duration);<br>    return;<br>  }<br>ret = av_seek_frame(fmt_ctx,audio_stream_index,start_pts,AVSEEK_FLAG_BACKWARD); //get one frame before timing to cover all<br>  if( ret < 0 ){<br>    fprintf(stderr,"av_seek_frame failed with error code %d\n",ret);<br>    return;<br>  }<br>  fprintf(stdout,"Start PTS: %lu End PTS: %lu\n",start_pts,end_pts);<br>  do{ //outer do-while to read packets<br>    ret = av_read_frame(fmt_ctx, &pkt);<br>    if(ret < 0){<br>      printf("Unable to read frame:%d \n",ret);<br>      break;<br>    }<br>    if(pkt.stream_index == audio_stream_index){ // processing audio packets<br>      size = pkt.size;<br>      while(size > 0){ // inner while to decode frames, if more than one are present in a single packet<br>    got_frame = 0;<br>    fprintf(stderr,"DEBUG: Packet PTS:%lu\n",pkt.pts);<br>    len = avcodec_decode_audio4(dec_ctx, frame, &got_frame, &pkt);<br>    if(len < 0){<br>      printf("Error while decoding\n");<br>    }<br>    if(got_frame){<br>      err = av_buffersrc_add_frame(src, frame);<br>      fprintf(stderr,"DEBUG: Frame added to buffer source with PTS %lu \n",frame->pts);<br>      if(err < 0) {<br>        av_frame_unref(frame);<br>        fprintf(stderr,"Error adding frame to source buffer\n");<br>        return;<br>      }<br>      size = size - len;<br>    }<br>   }<br>  }<br> }while(frame->pts < end_pts);<br><br> while((err = av_buffersink_get_frame(sink,frame)) >= 0){<br>   if(err < 0){<br>     av_strerror(err,errstr,128);<br>     fprintf(stderr,"av_buffer_get_frame returns %d %s\n",err,errstr);<br>     break;<br>   }<br>   fprintf(stdout,"frame_pts %lu frame->duration(PTS) %d frame->duration: %f with sample rate as:%d number of samples %d\n",frame->pts,frame->pkt_duration,((double)frame->pkt_duration/frame->sample_rate) * 1000,frame->sample_rate,frame->nb_samples);<br> }<br> <br> av_frame_free(&frame);<br> return;<br>}<br><br></div><div style="text-align:left">Regards<br></div></div>