2010-04-23 129 views

回答

0

这里是我做的,它为我的伟大工程。呼叫

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和解析它来确定源视频剪辑的大小。

8

为什么你想解析输出?而是使用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); 

我还没有包括在此代码的任何错误检查,你可以解决它自己。我希望这有帮助。

相关问题