User Tools

Site Tools


linuxvideocapture

I use a Magewell USB HDMI video capture device to grab the output from one system and record it on another. Typically this is useful for making screencasts.

screencast.sh
#!/bin/bash

VIDEODEVICE="/dev/video2"
DEVICE=$(lsusb | grep 2935\:0006)

if [[ "$?" == "0" ]]; then
  echo "Found Magewell device, resetting"
  BUS=$(echo $DEVICE | awk -F ' ' '{ print $2 }')
  DEV=$(echo $DEVICE | awk -F ' ' '{ print $4 }' | tr -d ':')
  sudo ./usbreset /dev/bus/usb/$BUS/$DEV
else
  echo "Can't find Magewell device, exiting"
  exit 1
fi

FILENAME=$HOME/Videos/Screencast-$(date +%Y-%m-%d-%H%M%S).mkv
  /usr/bin/ffmpeg \
  -thread_queue_size 512 \
  -r 60 \
  -f v4l2 \
  -video_size 1920x1080 \
  -i $VIDEODEVICE \
  -crf 0 \
  -c:v libx264rgb \
  -preset ultrafast \
  -threads 4  \
  $FILENAME
echo $FILENAME
echo mpv $FILENAME

In addition, there's a need for a `usbreset` command to hardware-reset the Magewell device. I got this from https://askubuntu.com/a/661/612

Compile it with

$ cc usbreset.c -o usbreset
usbreset.c
/* usbreset -- send a USB port reset to a USB device */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <linux/usbdevice_fs.h>


int main(int argc, char **argv)
{
    const char *filename;
    int fd;
    int rc;

    if (argc != 2) {
        fprintf(stderr, "Usage: usbreset device-filename\n");
        return 1;
    }
    filename = argv[1];

    fd = open(filename, O_WRONLY);
    if (fd < 0) {
        perror("Error opening output file");
        return 1;
    }

    printf("Resetting USB device %s\n", filename);
    rc = ioctl(fd, USBDEVFS_RESET, 0);
    if (rc < 0) {
        perror("Error in ioctl");
        return 1;
    }
    printf("Reset successful\n");

    close(fd);
    return 0;
}
linuxvideocapture.txt · Last modified: 2019/05/21 09:47 by popey