Search code examples
c++macosvideoffmpegopenframeworks

How to record output frames to a video file in OpenFrameworks OF 0.8.4 OSX


Hi I'm attempting to use this to record output of my openframeworks app.

I was trying to use this extension here. As there seems like there is no built in method for this.

https://github.com/timscaffidi/ofxVideoRecorder

I have it producing a video file, but it looks like one of those scrolling sidewalk adverts, the frames scroll upwards instead of proceeding like a normal video. Any idea why?

I had this at the end of my setup method.

if(!vidRecorder.isInitialized())
{
vidRecorder.setup(fileName+ofGetTimestampString()+fileExt, ofGetWidth(), ofGetHeight(), 30); // no audio

}

Now I had the window mode set to fullscreen intitially and that gave me scanlines on the output, so I made it not full screen and I got the frames I already mentioned and then I set it to 640, 480 and I got more scanlines.

I was using this code in my update loop

ofImage img;
img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());


vidRecorder.addFrame(img.getPixelsRef());

Here is the ffmpeg output.

[warning] ofThread: - name: Thread 4 - Calling startThread with verbose is deprecated.
[warning] ofThread: - name: Thread 2 - Calling startThread with verbose is deprecated.
ffmpeg version 2.6.1 Copyright (c) 2000-2015 the FFmpeg developers
  built with llvm-gcc 4.2.1 (LLVM build 2336.11.00)
  configuration: --prefix=/Volumes/Ramdisk/sw --enable-gpl --enable-pthreads --enable-version3 --enable-libspeex --enable-libvpx --disable-decoder=libvpx --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-avfilter --enable-libopencore_amrwb --enable-libopencore_amrnb --enable-filters --enable-libgsm --enable-libvidstab --enable-libx265 --disable-doc --arch=x86_64 --enable-runtime-cpudetect
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, rawvideo, from '/Users/benjgorman/Code/OF_ROOT/addons/ofxTimeline/duplicate_lips/bin/data/ofxvrpipe0':
  Duration: N/A, start: 0.000000, bitrate: 566231 kb/s
    Stream #0:0: Video: rawvideo (RGB[24] / 0x18424752), rgb24, 1024x768, 566231 kb/s, 30 tbr, 30 tbn, 30 tbc
Please use -b:a or -b:v, -b is ambiguous
Output #0, mov, to '/Users/benjgorman/Code/OF_ROOT/addons/ofxTimeline/duplicate_lips/bin/data/testMovie2015-04-16-11-21-31-504.mov':
  Metadata:
    encoder         : Lavf56.25.101
    Stream #0:0: Video: mpeg4 (mp4v / 0x7634706D), yuv420p, 1024x768, q=2-31, 800 kb/s, 30 fps, 15360 tbn, 30 tbc
    Metadata:
      encoder         : Lavc56.26.100 mpeg4
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg4 (native))
Press [q] to stop, [?] for help
frame=    9 fps=0.0 q=14.2 size=     861kB time=00:00:00.30 bitrate=23510.3kbits/s    
frame=   19 fps= 18 q=24.8 size=    1330kB time=00:00:00.63 bitrate=17202.4kbits/s    
frame=   31 fps= 20 q=24.8 size=    1864kB time=00:00:01.03 

//

more frames here omitted for clarity

//

frame=   87 fps= 16 q=24.8 size=    4349kB time=00:00:02.90 bitrate=12286.0kbits/s    
frame=   95 fps= 16 q=24.8 size=    4704kB time=00:00:03.16 bitrate=12169.1kbits/s    
frame=  103 fps= 16 q=24.8 size=    5061kB time=00:00:03.43 bitrate=12075.1kbits/s    
frame=  111 fps= 16 q=24.8 size=    5415kB time=00:00:03.70 bitrate=11990.0kbits/s    
frame=  118 fps= 15 q=24.8 size=    5726kB time=00:00:03.93 bitrate=11925.3kbits/s    
frame=  130 fps= 16 q=24.8 size=    6261kB time=00:00:04.33 bitrate=11835.3kbits/s    
[rawvideo @ 0x10201e000] Invalid buffer size, packet size 1179648 < expected frame_size 2359296
Error while decoding stream #0:0: Invalid argument
frame=  138 fps= 14 q=24.8 size=    6618kB time=00:00:04.60 bitrate=11785.4kbits/s    
frame=  138 fps= 14 q=24.8 Lsize=    6620kB time=00:00:04.60 bitrate=11788.8kbits/s    
video:6618kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.028923%

Is this an ffmpeg error? Or is there a better solution.


Solution

  • Could you suply the complete code? Are you using vidGrabber.update(); in the update?

    Why don't you use:

    ofApp.h

    ofVideoGrabber      vidGrabber;
    ofxVideoRecorder    vidRecorder;
    bool bRecording;
    void startRecord();
    void stopRecord();
    

    ofApp.c

    void ofApp::setup(){
        vidInput = ofPtr<ofQTKitGrabber>( new ofQTKitGrabber() );
        vidGrabber.setGrabber(vidInput);
        vidGrabber.initGrabber(1280, 720);
        vidRecorder.setFfmpegLocation(ofFilePath::getAbsolutePath("ASSETS/ffmpeg"));
    }
    
    void ofApp::update(){
        vidGrabber.update();
        if(vidGrabber.isFrameNew()){
            vidRecorder.addFrame(vidGrabber.getPixelsRef());
        }
    }
    
    void ofApp::startRecord() {
    
        bRecording = true;
    
        if(bRecording && !vidRecorder.isInitialized()) {
    
            vidRecorder.setup("your-file-name.mp4", vidGrabber.getWidth(), vidGrabber.getHeight(), 30, 44100, 2);
    
        }
    
    }
    //--------------------------------------------------------------
    void ofApp::stopRecord() {
    
        bRecording = false;
    
        vidRecorder.close();
    
    }