AMR音频文件的持续时间

问题描述:

我想查找类型为“amr”的音频文件的持续时间,而不将其转换为其他音频格式 以任何方式?AMR音频文件的持续时间

AK

我已经在objective-C中编码了以下内容以获得电影的持续时间。这同样可以用来获取音频的持续时间过:

-(double)durationOfMovieAtPath:(NSString*)inMoviePath 
{ 
    double durationToReturn = -1; 

    NSFileManager *fm = [NSFileManager defaultManager]; 
    if ([fm fileExistsAtPath:inMoviePath]) 
    { 
     av_register_all(); 

     AVFormatContext *inMovieFormat = NULL; 
     inMovieFormat = avformat_alloc_context(); 
     int errorCode = av_open_input_file(&inMovieFormat, [inMoviePath UTF8String], NULL, 0, NULL); 
     //double durationToReturn = (double)inMovieFormat->duration/AV_TIME_BASE; 

     if (0==errorCode) 
     { 
      // only on success 
      int numberOfStreams = inMovieFormat->nb_streams; 
      AVStream *videoStream = NULL; 
      for (int i=0; i<numberOfStreams; i++) 
      { 
       AVStream *st = inMovieFormat->streams[i]; 

       if (st->codec->codec_type == CODEC_TYPE_VIDEO) 
       { 
        videoStream = st; 
        break; 
       } 
      } 

      double divideFactor; 
      // The duraion in AVStream is set in accordance with the time_base of AVStream, so we need to fetch back the duration using this factor 
      divideFactor = (double)1/rationalToDouble(videoStream->time_base); 

      if (NULL!=videoStream) 
       durationToReturn = (double)videoStream->duration/divideFactor; 
      //DEBUGLOG (@"Duration of movie at path: %@ = %0.3f", inMoviePath, durationToReturn); 
     } 
     else 
     { 
      DEBUGLOG (@"avformat_alloc_context error code = %d", errorCode); 
     } 

     if (nil!=inMovieFormat) 
     { 
      av_close_input_file(inMovieFormat); 
      //av_free(inMovieFormat); 
     } 
    } 

    return durationToReturn; 
} 

更改CODEC_TYPE_VIDEOCODEC_TYPE_AUDIO,我认为它应该为你工作。

+0

好的谢谢!一些Java的东西? – Antonis 2010-09-20 14:10:13

+0

上面的代码也可以很容易地转换为java。该代码非常适合视频,我已经测试过它。请让我知道它是否与音频文件兼容。所有不同的是你选择的流。 – 2010-09-21 06:22:50

+0

好的,我会尝试! – Antonis 2010-09-21 10:01:08