views:

265

answers:

4

Hi Team,

I am uploading audio files in asp.net using FFMPEG.My question is how can i get the duration of the file(in seconds).

Please suggest me.

Thanks and Regards Srinivas M

A: 

Use the following command and then parse the output looking for 'Duration':

ffmpeg -i demo.mp3
zaf
Hi Zaf,I am doing the same for uploading the audios,but can you please elaborate how can i parse the output for getting the duration of audio file.Thanks in advanceregardsSrinivas
Seems like the other answers give you enough info to execute and parse the duration. If you still have problems then let us know.
zaf
A: 

Here's what I do and it works great for me. Call

ffmpeg -i District9.mov

Then find the length of the video in the below snippet with a regex or a simple string.startWith(" Duration:") type check:

Seems stream 0 codec frame rate differs from container frame rate: 5994.00 
(5994/1) -> 29.97 (30000/1001)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/stu/Movies/District9.mov':
  Duration: 00:02:32.20, start: 0.000000, bitrate: 9808 kb/s
    Stream #0.0(eng): Video: h264, yuv420p, 1920x1056, 29.97tbr, 2997tbn, 5994tbc
    Stream #0.1(eng): Audio: aac, 44100 Hz, 2 channels, s16
    Stream #0.2(eng): Data: tmcd / 0x64636D74

You'll should be able to consistently and safely find Duration: hh:mm:ss.nn and parse it to determine the source video clip size.

Stu Thompson
+2  A: 

Why do you want to parse the output? Instead use FFMpeg APIs to get the duration from the audio stream of the file. One cannot rely on the output strings, say the development team decides to change the logs in the future. So use APIs to get the duration.

Follow these steps:

1.  av_register_all();

2.  AVFormatContext * inAudioFormat = NULL;
    inAudioFormat = avformat_alloc_context();
    int errorCode = av_open_input_file(& inAudioFormat, "your_audio_file_path", NULL, 0, NULL);

3.  int numberOfStreams = inAudioFormat->nb_streams;
    AVStream *audioStream = NULL;
    for (int i=0; i<numberOfStreams; i++)
    {
        AVStream *st = inAudioFormat->streams[i];

        if (st->codec->codec_type == CODEC_TYPE_AUDIO)
        {
            audioStream = st;
            break;
        }
    }

4.  double divideFactor;
    divideFactor = (double)1/rationalToDouble(audioStream->time_base);

5.  double durationOfAudio = (double) audioStream->duration / divideFactor;

6.  av_close_input_file(inAudioFormat);

I havent included any error checks in this code, you can work it out for yourself. I hope this helps.

Raj
A: 

i want to implement ffmpeg in asp.net, how do i start please help.

yogs