I am using a Raspberry Pi 3 and a USB web cam to record video. I wrote a script that launches the camera and tells it how long to run for before closing. I wanted to be able to stop the recording earlier if I wanted so I hooked up a button to a GPIO pin and I am able to stop the recording early no problem. The next step is to transfer the .avi files from the Raspberry Pi to a Windows 7 laptop. I am doing this through a USB stick. I can successfully watch videos that recorded for the entire recording time uninterrupted but I am unable to render any video I stopped early. I can play both methods of recording back on the Raspberry Pi using omxplayer so I know the video files aren't empty. Does anyone have any idea on why the videos I stopped early would work on the Raspberry Pi but not a Windows 7 laptop?


EDIT: Here is how I launch and kill the recording script.

import RPi.GPIO as GPIO
import signal
import time
import os
import subprocess
from subprocess import call
import sys
from sys import exit


GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

set = 0
while True:
    if GPIO.input(12) == False and set == 0:
        print('Starting Video Recording')
        p=subprocess.Popen("./video", shell=False, preexec_fn=os.setsid)
        set = 1
    if GPIO.input(16) == False and set == 1:
        print('Stopping Video Recording')
        os.killpg(os.getpgid(p.pid), signal.SIGTERM)
        set = 0
    if GPIO.input(18) == False and set == 0:
        print('Exit Video Recording') 


#include "uEye.h"
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/core.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

HIDS hCam = 1;
SENSORINFO sensor_info;
CAMINFO camera_info;
int nRet;
int Mode;
char strCamFileName[256];
int img_width=640;
int img_height=480;

int main ()
//initialize camera
nRet = is_InitCamera(&hCam, NULL);
cout << "Status Init: " << nRet << endl;

//get sensor info
//nRet = is_GetSensorInfo(hCam, &sensor_info);
//cout << "Sensor Color Mode: " << sensor_info.nColorMode << endl;
//cout << "Camera Model: " << sensor_info.strSensorName << endl;

//get camera info
//nRet = is_GetCameraInfo(hCam, &camera_info);
//cout << "Camera ID: " << camera_info.ID << endl;
//cout << "Camera SerNum: " << camera_info.SerNo << endl;
//cout << "Camera Version: " << camera_info.Version << endl;
//cout << "Camera Type: " << camera_info.Type << endl;

//color mode
nRet = is_SetColorMode(hCam, Mode);
cout << "Color Mode: " << nRet << endl;

UINT formatID = 13;
nRet = is_ImageFormat(hCam, IMGFRMT_CMD_SET_FORMAT, &formatID, 4);
cout << "Status Image Format: " << nRet << endl;

char* pMem = NULL;
int memID = 0;
nRet = is_AllocImageMem(hCam, img_width, img_height, 24, &pMem, &memID);
nRet = is_SetImageMem(hCam, pMem, memID);

//set display mode
nRet = is_SetDisplayMode(hCam, Mode);

double dZoomValue = 0;
nRet = is_Zoom(hCam, ZOOM_CMD_DIGITAL_SET_VALUE, (void*)&dZoomValue, sizeof(dZoomValue));
cout << "Zoom: " << dZoomValue << endl;

VideoWriter video("out.avi", CV_FOURCC('X','V','I','D'), 10, Size(img_width, img_height),true);

for(int ii=0; ii<600; ii++)
   if(is_FreezeVideo(hCam, IS_WAIT) == IS_SUCCESS){
        void *pMemVoid; //pointer to where the image is stored
        is_GetImageMem (hCam, &pMemVoid);
        Mat img=Mat(Size(img_width,img_height), CV_8UC3, pMemVoid);
    namedWindow( "Live Video", WINDOW_NORMAL);
    resizeWindow("Live Video", 320,240);
    imshow("Live Video", img);

//exit camera

return 0;
  • How exactly do you stop the recording? If you do something like killall raspivid, you may be producing incomplete files, which Windows Media Player may refuse to play. Try using a different player like VLC or MPC. – Dmitry Grigoryev Apr 19 '17 at 11:47
  • I edited my post to include how I launch and terminate the script – eurotech Apr 19 '17 at 11:55
  • 1
    I can see how you're launching something - what program are you using to actually record the video, and what parameters are you using when you run it? This sounds very similar to a problem I ran up against a few years ago where I tried to do the same thing with VLC and killed the process before it wrote MOOV atoms into my video files, rendering the files unplayable. I had a fix at the time which I'm trying to track down - I think there's a workaround for knackered files (or you could just close your recorder properly!). – goobering Apr 19 '17 at 12:04
  • What is this mysterious "./video" command? – Dmitry Grigoryev Apr 19 '17 at 12:12
  • Video is the executable that I made when compiling my camera recording program. @goobering how do i close my recorder properly? – eurotech Apr 19 '17 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.