我使用FFMPEG.My在asp.net上傳音頻文件。我的問題是如何獲取文件的持續時間(以秒爲單位)。如何獲取使用FFMPEG上傳後的音頻文件的持續時間
請給我建議。
感謝和問候 SRINIVAS中號
我使用FFMPEG.My在asp.net上傳音頻文件。我的問題是如何獲取文件的持續時間(以秒爲單位)。如何獲取使用FFMPEG上傳後的音頻文件的持續時間
請給我建議。
感謝和問候 SRINIVAS中號
這裏是我做的,它爲我的偉大工程。呼叫
ffmpeg -i District9.mov
然後找到視頻的長度在下面的代碼片段,其中一個正則表達式或簡單string.startWith(" Duration:")
類型檢查:
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
你應該能夠持續,安全地找到Duration: hh:mm:ss.nn
和解析它來確定源視頻剪輯的大小。
爲什麼你想解析輸出?而是使用FFMpeg API從文件的音頻流中獲取持續時間。人們不能依賴輸出字符串,比方說開發團隊決定在將來更改日誌。所以使用API來獲取持續時間。
遵循以下步驟:
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);
我還沒有包括在此代碼的任何錯誤檢查,你可以解決它自己。我希望這有幫助。