Hi there,
I am now able to record audio and video together in a quicktime file.
I’ve used libsndfile from http://www.mega-nerd.com/libsndfile/ .
and used ofxvideosaver. I’ve modified some codes from http://forum.openframeworks.cc/t/recording-quicktime-with-sound-in-sync/775/0
How it works is:
#include "testApp.h"
#include "stdio.h"
#include <sndfile.h>
static bool bRecording = false;
//--------------------------------------------------------------
void testApp::setup(){
camWidth = 320; // try to grab at this size.
camHeight = 240;
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(camWidth,camHeight);
saver.listCodecs();
saver.setCodecType(17); //ZACH FIX see recording quicktime with sound in sync on of forum
saver.setCodecQualityLevel(OF_QT_SAVER_CODEC_QUALITY_NORMAL);
saver.setup(320,240,"data/output.mov");
ofSetFrameRate(30);
ofBackground(255,255,255);
// 0 output channels,
// 2 input channels
// 22050 samples per second
// 256 samples per buffer
// 4 num buffers (latency)
ofSoundStreamSetup(0,2,this, 44100, 256, 4);
left = new float[256];
right = new float[256];
bufferCounter = 0;
drawCounter = 0;
sampleRate = 44100;
info.format=SF_FORMAT_WAV | SF_FORMAT_PCM_16;
info.frames = sampleRate*30;
info.samplerate = sampleRate;
info.channels = 2;
outfile = sf_open ("data/mydata.wav", SFM_WRITE, &info) ;
if (!outfile)
{
cerr<<"Error opening ["<<outfilename<<"] : "<<sf_strerror (outfile)<<endl;
}
}
//--------------------------------------------------------------
void testApp::draw(){
vidGrabber.draw(20,400);
// draw the left:
ofSetColor(0x333333);
ofRect(100,100,256,200);
ofSetColor(0xFFFFFF);
for (int i = 0; i < 256; i++){
ofLine(100+i,200,100+i,200+left[i]*100.0f);
}
// draw the right:
ofSetColor(0x333333);
ofRect(600,100,256,200);
ofSetColor(0xFFFFFF);
for (int i = 0; i < 256; i++){
ofLine(600+i,200,600+i,200+right[i]*100.0f);
}
ofSetColor(0x333333);
drawCounter++;
char reportString[255];
sprintf(reportString, "buffers received: %i\ndraw routines called: %i\n", bufferCounter,drawCounter);
ofDrawBitmapString(reportString,80,380);
}
void testApp::update(){
ofBackground(100,100,100);
vidGrabber.grabFrame();
if (bRecording == true){
saver.addFrame(vidGrabber.getPixels(), 1.0f / 30.0f);
// you can also pass in the frameRate:
// saver.addFrame(vidGrabber.getPixels(), 1.0f / 30.0f); // 30 fps for this frame
// saver.addFrame(vidGrabber.getPixels(), 1.0f / 5.0f); // 5 fps for this frame, etc....
}
}
static float adder = 0;
//--------------------------------------------------------------
void testApp::audioReceived (float * input, int bufferSize, int nChannels){
if (bRecording){
// samples are "interleaved"
for (int i = 0; i < bufferSize; i++){
left[i] = input[i*2];
right[i] = input[i*2+1];
}
bufferCounter++;
printf("%i\n",bufferSize);
sf_write_float(outfile, input, bufferSize*2);
}
}
//--------------------------------------------------------------
void testApp::keyPressed (int key){
if (key=='a')
{
bRecording = true;
}
if (key=='s')
{
bRecording = false;
sf_close(outfile);
}
if (key==' ')
{
saver.addAudioTrack("data/mydata.wav");
saver.finishMovie();
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::resized(int w, int h){
}
now my problem is syninc. I have an empty frame at the end of the mov file, and the sound suddenly stops. Any ideas?