使用FFmpegFrameGrabber,android中的FrameRecorder JavaCv创建的视频中没有捕获声音

时间:2014-11-05 11:00:39

标签: android opencv ffmpeg video-processing javacv

我正在尝试使用android中的 JavaCV 将灰度效果应用于视频文件.Everything工作正常,但输出文件中没有音频。下面是代码。请帮忙

   File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test3.mp4"); 

     FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file.getAbsolutePath()); 

     FrameRecorder recorder = null; 

     Log.d("bharat", " Audio channels = " + frameGrabber.getAudioChannels()); // THIS IS RETurnING 0 and not 2

     recorder = new FFmpegFrameRecorder("/mnt/sdcard/streaml_t.mp4", 270, 480, frameGrabber.getAudioChannels()); 

     recorder.setVideoCodec(AV_CODEC_ID_H264); 

     recorder.setFormat("mp4"); 

     recorder.setFrameRate(frameGrabber.getFrameRate()); 

     recorder.setSampleFormat(frameGrabber.getSampleFormat()); 

     recorder.setSampleRate(frameGrabber.getSampleRate()); 

     try { 
     recorder.start();
     frameGrabber.start(); 
     int count = 0;      
     while (true) { 

     try { 

     Frame grabFrame = frameGrabber.grabFrame(); 
     Log.d("bharat:", "frame " + count++); 

     if (grabFrame == null) { 
     System.out.println("!!! Failed cvQueryFrame"); 
     break; 

     } 

     IplImage frame_copy = cvCreateImage(cvSize(grabFrame.image.width(), grabFrame.image.height()), IPL_DEPTH_8U, 1); 

     cvCvtColor(grabFrame.image, frame_copy, CV_RGB2GRAY); 
     grabFrame.image = frame_copy; 
     recorder.setTimestamp(frameGrabber.getTimestamp());
     recorder.record(grabFrame); 
     } catch (Exception e) { 
        e.printStackTrace();
     } 

     } 

     Log.d("bharat:", "frame done"); 

     recorder.stop(); 
     recorder.release(); 

     } catch (Exception e) { 

     e.printStackTrace(); 

     } 

     }

P上。 S:我发现frameGrabber.getAudioChannels()返回0而不是2(如果它有帮助)

1 个答案:

答案 0 :(得分:1)

由于缺少JavaCV中的文档而导致问题met如下:

/**  Grab next videoframe */
public IplImage grab() 
/** Grab next video or audio frame */
public Frame grabFrame()

因此,您获得音频或视频帧,然后您总是尝试将其作为图像处理。您应该先检查音频或视频帧,不要尝试为音频调用cvCvtColor()。