سؤال

أريد أن أجد مدة ملف صوتي من النوع "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 وأعتقد أنه ينبغي أن يعمل من أجلك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top