linux - amr 音频文件的持续时间

标签 linux ffmpeg 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_VIDEO 更改为 CODEC_TYPE_AUDIO,我认为它应该适合您。

关于linux - amr 音频文件的持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3736009/

相关文章:

javascript - 使用 node cli 工具作为库 (trim3)

audio - 如何在 OS X 上将 audio.m4a 转换为 audio.amr?

ios - xCode 4.3.2 链接错误,但 arch 确实存在。为什么?

linux - MongoDB 使用了太多内存

android - 检查android中的处理器功能

ffmpeg - 使用#EXT-X-DISCONTINUITY-SEQUENCE [ffmpeg] 下载 ts 流

blackberry - 外部播放BlackBerry上录制的音频

linux - 从 postgresql 数据库中的表中选择 linux 上的 cobol 程序时出错

node.js - 在 Ubuntu 16.10 上安装 nodejs 的问题

linux - xargs 并查找,rm 提示文件名中的\n(换行符)