在c ++中从execve调用时,ffmpeg会产生错误的输出

时间:2016-05-28 03:33:56

标签: c++ video ffmpeg fork execve

我正在编写一个涉及从c ++程序调用ffmpeg的c ++程序。几天前我使用std :: system

工作了
std::system("ffmpeg -threads auto -y -r 1.74659 -i /mnt/ev_ramdsk/1/%05d-capture.jpg -px_fmt yuv420p -preset ultrafast -r 10 /mnt/ev_ramdsk/1/video.mp4");

但这只能工作一次现在这会产生8MB左右的.mp4视频,无法在任何地方播放..所以由于前一个问题的建议,我感动了。

这是我的代码

child_pid = fork();
        if(child_pid < 0){
            syslog(LOG_ERR, "ERROR: ffmpeg forking failed");
            return false;
        }
        else if(child_pid > 0){
            syslog(LOG_DEBUG, "DEBUG: forking succeeded, pid: %d", child_pid);
        }
        else if(child_pid == 0){
            char *newargv[16];
            for(int i=0; i < 15; i++) newargv[i] = (char *)malloc(sizeof(char) * 60); //allocate the array in memory
            strcpy(newargv[0], "/usr/bin/ffmpeg");
            strcpy(newargv[1], "-threads");
            strcpy(newargv[2], "auto");
            strcpy(newargv[3], "-y");
            strcpy(newargv[4], "-framerate");
            tempSS << fps;
            strcpy(newargv[5], tempSS.str().c_str());
            tempSS.str(std::string());
            strcpy(newargv[6], "-i");
            strcpy(newargv[7], std::string(conf->dir_ram + dest + "%05d-capture.jpg").c_str());
            strcpy(newargv[8], "-pix_fmt");
            strcpy(newargv[9], "yuv420p");
            strcpy(newargv[10], "-preset");
            strcpy(newargv[11], "ultrafast");
            strcpy(newargv[12], "-r");
            strcpy(newargv[13], "25");
            strcpy(newargv[14], std::string(conf->dir_ram + dest + "video.mp4").c_str());
            newargv[15] = NULL;

            for(int i=0; i < 15; i++){
                tempSS << "newargv[" << i << "] = \"" << newargv[i] << "\", ";
            }
            syslog(LOG_DEBUG, "DEBUG:newargv: %s", tempSS.str().c_str());
            tempSS.str(std::string());

            char *newenviron[] = { NULL };

            if(execve(newargv[0], newargv, newenviron) == -1){
                syslog(LOG_ERR, "ERROR: execve returned -1");
                exit(EXIT_SUCCESS);
            }
        }

        wpid = wait(&status);
        syslog(LOG_DEBUG, "DEBUG: ffmpeg child terminated, pid: %d, status: %d", wpid, status);

syslog的输出是:

May 28 00:25:03 SERVER dt_ev_maker[10471]: DEBUG: forking succeeded, pid: 10658
May 28 00:25:03 SERVER dt_ev_maker[10658]: DEBUG:newargv: 
newargv[0] = "/usr/bin/ffmpeg", 
newargv[1] = "-threads", 
newargv[2] = "auto", 
newargv[3] = "-y", 
newargv[4] = "-framerate", 
newargv[5] = "1.45097", 
newargv[6] = "-i", 
newargv[7] = "/mnt/ev_ramdsk/1/%05d-capture.jpg", 
newargv[8] = "-pix_fmt", 
newargv[9] = "yuv420p", 
newargv[10] = "-preset", 
newargv[11] = "ultrafast", 
newargv[12] = "-r", 
newargv[13] = "25", 
newargv[14] = "/mnt/ev_ramdsk/1/video.mp4",
May 28 00:25:03 SERVER dt_ev_maker[10471]: DEBUG: ffmpeg child terminated, pid: 10658, status: 256

在这种情况下,视频大小约为90B,也已损坏。

注意:如果我从命令行运行相同的命令,视频可以正常播放。

我做错了什么?

提前致谢!

修改

感谢ouroborus(上面提交的更改),我让它制作了18MB视频,但我无法播放它们。

2 个答案:

答案 0 :(得分:1)

似乎execve()需要它的数组才能拥有NULL的最后一个元素。将您的newargv数组扩展为1,并将最后一个元素设置为NULL,然后再将其传递到execve()

参考:Using execve for the first time

答案 1 :(得分:1)

所以,最终起作用的代码是:

pid_t wpid, child_pid;
            int status;
            int fd = open(std::string(conf->dir_ram + "ffmpeg.txt").c_str(), O_CREAT | O_WRONLY, 0777);
            if(fd < 0){
                syslog(LOG_ERR, "ERROR: open returned -1");
                return false;
            }

            child_pid = fork();
            if(child_pid < 0){
                syslog(LOG_ERR, "ERROR: ffmpeg forking failed");
                return false;
            }
            else if(child_pid > 0){
                syslog(LOG_DEBUG, "DEBUG: forking succeeded, pid: %d", child_pid);
            }
            else if(child_pid == 0){

                if( close(1) < 0 ){
                    syslog(LOG_ERR, "ERROR: close returned -1");
                    return false;
                }
                if( dup2(fd, 1) < 0 ){
                    syslog(LOG_ERR, "ERROR: dup2 returned -1");
                    return false;
                }

                char *newargv[18];
                for(int i=0; i < 17; i++) newargv[i] = (char *)malloc(sizeof(char) * 60); //allocate the array in memory
                strcpy(newargv[0], conf->ffmpeg_path.c_str());
                strcpy(newargv[1], "-threads");
                strcpy(newargv[2], "auto");
                strcpy(newargv[3], "-y");
                strcpy(newargv[4], "-r");
                tempSS << fps;
                strcpy(newargv[5], tempSS.str().c_str());
                tempSS.str(std::string());
                strcpy(newargv[6], "-i");
                strcpy(newargv[7], std::string(conf->dir_ram + dest + "%05d-capture.jpg").c_str());
                strcpy(newargv[8], "-pix_fmt");
                strcpy(newargv[9], "yuv420p");
                strcpy(newargv[10], "-c:v");
                strcpy(newargv[11], "libx264");
                strcpy(newargv[12], "-preset");
                strcpy(newargv[13], "ultrafast");
                strcpy(newargv[14], "-r");
                strcpy(newargv[15], conf->ffmpeg_rate.c_str());
                strcpy(newargv[16], std::string(conf->dir_ram + dest + "video.mp4").c_str());
                newargv[17] = NULL;

                char *newenviron[] = { NULL };

                if(execve(newargv[0], newargv, newenviron) == -1){
                    syslog(LOG_ERR, "ERROR: execve returned -1");
                    exit(EXIT_SUCCESS);
                }
            }

            wpid = wait(&status);

如果我评论整个open()和dup2()部分,视频会被破坏。我不明白为什么,但没有它它不起作用。