Segmentation Fault with external program (ffmpeg)

Hi, I am trying to launch ffmpeg from the program that I’m writing, but when I call it through a keyPressed the program crashes with a segmentation fault. Do I have to allocate for ffmpeg?

Calling ffmpeg with system needs a full path, this works for me on Mac OS.

void ofApp::keyPressed(int key){
    system("/usr/local/Cellar/ffmpeg/3.3.4/bin/ffmpeg -formats");

Prints (surprise) all available formats.

Mine lloks like this:

void ofApp::keyPressed(int key){

        system("/home/antonio/bin/ffmpeg -format_code 4k30 -f decklink -i 'DeckLink Mini Recorder 4K' -vf \"trigger=delay=1:wait=2000\" -vsync 0 -an -y /home/antonio/Desktop/ffmpeg-prueba/last%04d.jpg");

still crashes with the segmentation fault.

I am not sure of the current state, but about a year ago I had to compile ffmpeg myself to be able to get the decklink capture option. Can you confirm you can capture with decklink from the command line without OF?

What platform are you working on, there are a few differences between mac and windows I am aware of, most relevant for windows, once you start ffmpeg from the console it blocks your app, and also cannot be stopped with ofSystem. I have a work around if you are on windows for the stopping and, I made the ffmpeg call in another thread which stopped it blocking my app.

Thank you for your reply. Yes, decklink is not out of the box, I had to compile from source and run a special configuration but it now works from the command line with no problems. I’m working on ubuntu 18.04, I don’t know how that affects the workflow :confused:

Maybe it has something to do with permission (to use ffmpeg?)
Using sudo visudo in Terminal to add the following line to the file
changes the behaviour of

void ofApp::keyPressed(int key){
    system("sudo id -u -r");

sudo: no tty present and no askpass program specified
0 (Same behaviour as in Terminal)
Not sure if this is very dumb or an actual solution :smile: Messing with sudo settings doesn’t seem like a good idea :grin:

It didn’t work, first I ran “sudo id -u -r” at the beginning and then “/local/directory/ffmpeg -formats” and the crash was keeping the camera busy when I tried again, so I had to reboot every time I wanted to try something new, and in the end I just tried the second command to see if it was a problem with launching external applications and it seems to be the case, even

system("ffmpeg -formats"); 

ends with a segmentation fault. Any ideas?

i doubt the crash is related to the system call. a system call is just starting a new process out of your own and even if it crashed your’s should be unaffected

have you tried debugging the application to see where the crash is happening?

Thanks for your reply. I tried but I don’t really know what to look for, the debugger points to an address in memory but I have no idea what to do next, or how to proceed. Do you know of any guide that I could read to understand better the whole debugging process? Also, I just tried with other commands and even a system("pwd"); makes the program crash. Maybe I have to declar something before using the system function?

i don’t think system is related at all, you don’t need to call anything before using it. the problem is surely that system is allocating a bit more memory and that is triggering a problem really caused somewhere else that doesn’t happen unless you use the system call.

when debugging look for the list of function calls when the program crashes, in that list go down until you find something that is happening in your program and that will give you a clue of what’s going on.

The process is pretty similar no matter what IDE you are using but perhaps google for something related to your IDE which will have images of where to find things

I think I know where the problem is, but I don’t know how to proceed. I’ll try to explain my setup as clear as possible.

Basically I am using a Blackmagic camera with a capture card and ofxBlackmagic to get a live feed in my program. I want to have a function that initiates ffmpeg externally, because I am using a special implementation of it that captures frames when in gets triggered by an arduino through the serial port.

In order to capture video with ffmpeg, I have to kill the live feed in my program because the video stream can only be accessed by one process at the time. One solution that was suggested in another post was to, instead of creating a ofxBlackMagic object, to use a shared pointer and to use the reset method right before calling ffmpeg. I think this is what is making my program crash, because if I access the live feed through the ofxBlackmagic object the program says that ffmpeg cannot access the camera with the following error, but at least it doesn’t crash with the segmentation error:

[decklink @ 0x56291e106360] Found Decklink mode 3840 x 2160 with rate 30.00

[decklink @ 0x56291e106360] Cannot enable video input

DeckLink Mini Recorder 4K: Input/output error

Should I stick to the shared pointer or is there a way to destroy my ofxBlackmagic object to allow ffmpeg to do it’s work?

That sounds like bugs in the ofxBlackMagic addon, it’s probably not closing the camera correctly and also not managing memory properly so when it’s destroyed it crashes. If you don’t know how to fix this problems you can try opening an issue in the addons github page

I will do that. In the meantime however, I just realized that I might be overlooking something. My keypressed event looks like this:

void ofApp::keyPressed(int key){

    if(key == 'h'){

        system("/home/antonio/bin/ffmpeg -format_code 4k30 -f decklink -i 'DeckLink Mini Recorder 4K' -vf \"trigger=delay=1:wait=2000\" -vsync 0 -an -y /home/antonio/Desktop/ffmpeg-prueba/last%04d.jpg");


The logic of the usleep was to give the program some time between one thing an the other just in case, to make sure that the feed is closed properly. However, it seems to be instantaneous, because the moment I press ‘h’ and switch back to the console ffmpeg has already failed. I’m I using usleep right?

Quick update:

I thought usleep worked with miliseconds, not microseconds. I changed 3000 for 3000000 and it does work, but ffmpeg still doesn’t manage to access the camera.

Are you only capturing a still image with ffmpeg?

No, the idea is to be able to capture video and output multiple files at once.

This may be another work around, if the release of the decklink hardware is not working correctly (PS which add-on are you using). Ffmpeg now has NDI support. You can record and output NDI at the same time. You can use the NDI output as you feed to you OF app and let ffmpeg take care of the Blackmagic hardware completely.

That sounds very interesting, how would you import an NDI output into an OF app? Is there a special addon for that?

This version of ofxndi apparently works on Linux.

Thank you for the help. I tried setting it up but I didn’t manage to configure my ffmpeg properly. This might be way simpler than I think but I just cannot manage to put it all together. I am using ofxBlackmagic on Ubuntu and the part of grabbing the signal from my capture card works perfectly. The problem is that when I want to start using ffmpeg and close the capture card, it seems that for some reason the program keeps the card busy and ffmpeg fails because it cannot access it. I tried ofxBlackmagic2 but it doesn’t seem to work with Ubuntu, and ofVideoGrabber doesn’t list the capture card as a device (how does it know what devices are available?). There has to be a way around it. FFplay also opens the camera very easily, but I would rather have it inside my program than as an external application.