Open-source News

How I use my old camera as a webcam with Linux

opensource.com - Mon, 12/19/2022 - 16:00
How I use my old camera as a webcam with Linux tomoliver Mon, 12/19/2022 - 03:00

This year after largely abandoning my MacBook in favor of a NixOS machine, I started getting requests to "turn my camera on" when video calling people. This was a problem because I didn't have a webcam. I thought about buying one, but then I realized I had a perfectly good Canon EOS Rebel XS DSLR from 2008 lying around on my shelf. This camera has a mini-USB port, so naturally, I pondered: Did a DSLR, mini-USB port, and a desktop PC mean I could have a webcam?

There's just one problem. My Canon EOS Rebel XS isn't capable of recording video. It can take some nice pictures, but that's about it. So that's the end of that.

Or is it?

There happens to be some amazing open source software called gphoto2. Once installed, it allows you to control various supported cameras from your computer and it takes photos and videos.

Supported cameras

First, find out whether yours is supported:

$ gphoto2 --list-camerasCapture an image

You can take a picture with it:

$ gphoto2 --capture-image-and-download

The shutter activates, and the image is saved to your current working directory.

Capture video

I sensed the potential here, so despite the aforementioned lack of video functionality on my camera, I decided to try gphoto2 --capture-movie. Somehow, although my camera does not support video natively, gphoto2 still manages to spit out an MJPEG file!

On my camera, I need to put it in "live-view" mode before gphoto2 records video. This consists of setting the camera to portrait mode and then pressing the Set button so that the viewfinder is off and the camera screen displays an image. Unfortunately, though, this isn't enough to be able to use it as a webcam. It still needs to get assigned a video device, such as /dev/video0.

Install ffmpeg and v4l2loopback

Not surprisingly, there's an open source solution to this problem. First, use your package manager to install gphoto2, ffmpeg, and mpv. For example, on Fedora, CentOS, Mageia, and similar:

$ sudo dnf install gphoto2 ffmpeg mpv

On Debian, Linux Mint, and similar:

$ sudo apt install gphoto2 ffmpeg mpv

I use NixOS, so here's my configuration:

# configuration.nix
...
environment.systemPackages = with pkgs; [
  ffmpeg
  gphoto2
  mpv
...

Creating a virtual video device requires the v4l2loopback Linux kernel module. At the time of this writing, that capability is not included in the mainline kernel, so you must download and compile it yourself:

$ git clone https://github.com/umlaeute/v4l2loopback

$ cd v4l2loopback

$ make

$ sudo make install

$ sudo depmod -a

If you're using NixOS like me, you can just add the extra module package in configuration.nix:

[...]
boot.extraModulePackages = with config.boot.kernelPackages;
[ v4l2loopback.out ];
boot.kernelModules = [
  "v4l2loopback"
];
boot.extraModprobeConfig = ''
  options v4l2loopback exclusive_caps=1 card_label="Virtual Camera"
'';
[...]

On NixOS, run sudo nixos-rebuild switch and then reboot.

Create a video device

Assuming your computer currently has no /dev/video device, you can create one on demand thanks to the v4l2loopback.

Run this command to send data from gphoto2 to ffmpeg, using a device such as /dev/video0 device:

$ gphoto2 --stdout --capture-movie |
 ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -f v4l2 /dev/video0

You get output like this:

ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 11.3.0 (GCC)
  configuration: --disable-static ...
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Capturing preview frames as movie to 'stdout'. Press Ctrl-C to abort.
[mjpeg @ 0x1dd0380] Format mjpeg detected only with low score of 25, misdetection possible!
Input #0, mjpeg, from 'pipe:':
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc, bt470bg/unknown/unknown), 768x512 ...
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> rawvideo (native))
[swscaler @ 0x1e27340] deprecated pixel format used, make sure you did set range correctly
Output #0, video4linux2,v4l2, to '/dev/video0':
  Metadata:
    encoder         : Lavf58.76.100
  Stream #0:0: Video: rawvideo (I420 / 0x30323449) ...
    Metadata:
      encoder         : Lavc58.134.100 rawvideo
frame=  289 fps= 23 q=-0.0 size=N/A time=00:00:11.56 bitrate=N/A speed=0.907x

To see the video feed from your webcam, use mpv:

$ mpv av://v4l2:/dev/video0 --profile=low-latency --untimed Image by:

(Tom Oliver, CC BY-SA 4.0)

Start your webcam automatically

It's a bit annoying to execute a command every time you want to use your webcam. Luckily, you can run this command automatically at startup. I implement it as a systemd service:

# configuration.nix
...
  systemd.services.webcam = {
    enable = true;
    script = ''
      ${pkgs.gphoto2}/bin/gphoto2 --stdout --capture-movie |
        ${pkgs.ffmpeg}/bin/ffmpeg -i - \
            -vcodec rawvideo -pix_fmt yuv420p -f v4l2  /dev/video0
    '';
wantedBy = [ "multi-user.target" ];
  };

...

On NixOS, run sudo nixos-rebuild switch and then reboot your computer. Your webcam is on and active.

To check for any problems, you can use systemctl status webcam. This tells you the last time the service was run and provides a log of its previous output. It's useful for debugging.

More Linux resources Linux commands cheat sheet Advanced Linux commands cheat sheet Free online course: RHEL technical overview Linux networking cheat sheet SELinux cheat sheet Linux common commands cheat sheet What are Linux containers? Our latest Linux articles Iterating to make it better

It's tempting to stop here. However, considering the current global crises, it may be pertinent to wonder whether it's necessary to have a webcam on all the time. It strikes me as sub-optimal for two reasons:

  1. It's a waste of electricity.
  2. There are privacy concerns associated with this kind of thing.

My camera has a lens cap, so to be honest, the second point doesn't really bother me. I can always put the lens cap on when I'm not using the webcam. However, leaving a big power-hungry DSLR camera on all day (not to mention the CPU overhead required for decoding the video) isn't doing anything for my electricity bill.

The ideal scenario:

  • I leave my camera plugged in to my computer all the time but switched off.
  • When I want to use the webcam, I switch on the camera with its power button.
  • My computer detects the camera and starts the systemd service.
  • After finishing with the webcam, I switch it off again.

To achieve this, you need to use a custom udev rule.

A udev rule tells your computer to perform a certain task when it discovers that a device has become available. This could be an external hard drive or even a non-USB device. In this case, you need it to recognize the camera through its USB connection.

First, specify what command to run when the udev rule is triggered. You can do that as a shell script (systemctl restart webcam should work). I run NixOS, so I just create a derivation (a Nix package) that restarts the systemd service:

# start-webcam.nix
with import <nixpkgs> { };

writeShellScriptBin "start-webcam" ''
  systemctl restart webcam
  # debugging example
  # echo "hello" &> /home/tom/myfile.txt
  # If myfile.txt gets created then we know the udev rule has triggered properly
''

Next, actually define the udev rule. Find the device and vendor ID of the camera. Do this by using the lsusb command. That command is likely already installed on your distribution, but I don't use it often, so I just install it as needed using nix-shell:

$ nix-shell -p usbutils

Whether you already have it on your computer or you've just installed it, run lsusb:

$ lsusb
Bus 002 Device 008: ID 04a9:317b Canon, Inc. Canon Digital Camera
[...]

In this output, the vendor ID is 04a9 and the device ID is 317b. That's enough to create the udev rule:

ACTION=="add", SUBSYSTEM=="usb",
ATTR{idVendor}=="04a9",
ATTR{idProduct}=="317b",
RUN+="/usr/local/bin/start-webcam.sh"

Alternatively, if you're using NixOS:

# configuration.nix
[...]
let
  startWebcam = import ./start-webcam.nix;
[...]
services.udev.extraRules = ''
  ACTION=="add",  \
  SUBSYSTEM=="usb", \
  ATTR{idVendor}=="04a9", \
  ATTR{idProduct}=="317b",  \
  RUN+="${startWebcam}/bin/start-webcam"
'';
[...]

Finally, remove the wantedBy = ["multi-user.target"]; line in your start-webcam systemd service. (If you leave it, then the service starts automatically when you next reboot, whether the camera is switched on or not.)

Reuse old technology

I hope this article has made you think twice before chucking some of your old tech. Linux can breathe life back into technology, whether it's your computer or something simple like a digital camera or some other peripheral.

I gave my old DSLR camera new life with gphoto2 by turning it into a webcam for my Linux computer.

Image by:

Internet Archive Book Images. Modified by Opensource.com. CC BY-SA 4.0

Linux Hardware What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

Discover the power of the Linux SpaceFM file manager

opensource.com - Mon, 12/19/2022 - 16:00
Discover the power of the Linux SpaceFM file manager sethkenlon Mon, 12/19/2022 - 03:00

SpaceFM is a tabbed file manager for Linux using the GTK toolkit, so it fits right in on desktops like GNOME, Mate, Cinnamon, and others. SpaceFM also features a built-in device manager system, so it's particularly good for window managers, like Fluxbox or fvwm, which typically don't include a graphical device manager. If you're happy with the file managers on Linux, but you want to try one that's a little bit different in design, SpaceFM is worth a look.

Install SpaceFM

On Linux, you're likely to find SpaceFM in your distribution's software repository. On Fedora, Mageia, OpenMandriva, and similar:

$ sudo dnf install spacefm

On Debian and Debian-based systems:

$ sudo apt install spacefmPanels

I don't know why SpaceFM is called SpaceFM, but it could be because it makes a concerted effort to let you use every bit of space in its window for something useful. By default, SpaceFM is actually pretty simple, standard-issue file manager. It has a single panel listing your files, a toolbar, and a menu bar.

Image by:

(Seth Kenlon, CC BY-SA 4.0)

All the "usual" rules apply.

  • Double-click to open a directory or to open a file in its default application.

  • Right-click for a contextual menu providing lots of standard options (copy, paste, rename, view properties, create a new folder, and so on).

The way SpaceFM sets itself apart, though, is its panel system. SpaceFM displays one panel by default. That's the big file window listing your files. But it can have up to four panel views, plus a few bonus panels for some specific tasks.

More Linux resources Linux commands cheat sheet Advanced Linux commands cheat sheet Free online course: RHEL technical overview Linux networking cheat sheet SELinux cheat sheet Linux common commands cheat sheet What are Linux containers? Our latest Linux articles Opening a new panel

Instead of seeing one directory in your file manager, you can see two. To bring up another directory in its own pane, press Ctrl+2 or go to the View menu and select Panel 2. Alternatively, click the second green dot icon from the left in the menu panel.

With two panels, you can move files from one directory to another without opening a new file manager window, or you can browse two directories to compare their contents.

But why settle for two panels? Maybe you'd rather see three directories at once. To bring up a third directory in a dedicated pane, press Ctrl+3 or go to the View menu and select Panel 3. Alternatively, click the third green dot icon from the left in the menu panel. This panel appears at the bottom of the SpaceFM window.

With three panels open, you can move files between several directories, or sort files from a common "dumping ground" (like your Desktop or Downloads folder) into specific directories.

Of course, once you've tried three panels you'll probably find yourself itching for a fourth. To open a fourth directory in its own pane, press Ctrl+4 or go to the View menu and select Panel 4. Alternatively, click the fourth green dot icon from the left in the menu panel. This one opens next to Panel 3, splitting your SpaceFM window into even quarters.

Image by:

(Seth Kenlon, CC BY-SA 4.0)

What about a fifth panel? Well, actually SpaceFM stops at four panels. If you really do want a fifth panel, you have to open a new SpaceFM window. However, there are still more panels, used for information other than file listings, to explore.

Special panels

The View menu reveals that in addition to file panels, there are additionally task-specific panels you can choose to display. This includes:

  • Task manager: Lists ongoing file manager processes. This isn't a general-purpose task manager, so to set nice values or detect a zombie apocalypse of undead PIDs, htop or top is still your utility of choice.

  • Bookmarks: Links to common folders, such as Desktop, Documents, Downloads, and any location you want to keep handy.

  • Devices: USB thumb drives and remote file systems.

  • File tree: A view of your file system in order of directory inheritance.

These panels open on the left side of SpaceFM, but they do stack. You can have bookmarks, devices, tasks, and a file tree open at once, although it helps to have a very tall SpaceFM window.

Make space for SpaceFM

SpaceFM is a configurable multi-tasking file manager. It maximizes the information you can build into a single window, and it lets you decide what's important, and when. This article has focused on the panels of SpaceFM because those are, at least in my view, the most unique aspect of the application. However, there's a lot more to SpaceFM, including plugins, preferences, a design mode, keyboard shortcuts, and customization. This isn't a small application, even though it is a lightweight one. Spend some time with SpaceFM, because you never know what you'll discover.

If you're happy with the file managers on Linux, but you want to try one that's a little bit different in design, SpaceFM is worth a look.

Image by:

Opensource.com

Linux What to read next This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

Most Commonly Asked Questions in Linux Interviews

Tecmint - Mon, 12/19/2022 - 15:14
The post Most Commonly Asked Questions in Linux Interviews first appeared on Tecmint: Linux Howtos, Tutorials & Guides .

If you have already achieved your Linux certification and are looking forward to securing a Linux job, it pays a great deal to prepare for an interview that tests your knowledge of the ins

The post Most Commonly Asked Questions in Linux Interviews first appeared on Tecmint: Linux Howtos, Tutorials & Guides.

XFS Aims To Get Online File-System Repair Support Merged Next Year

Phoronix - Mon, 12/19/2022 - 13:00
For years XFS has been working toward online repair capabilities for the file-system and it looks like in 2023 that work may finally come to fruition...

GNU Linux-libre's Freed-ora Effort To "Free Fedora" Has Been Sunset

Phoronix - Mon, 12/19/2022 - 04:10
Freed-ora had been a seldom talked about effort from the Free Software Foundation Latin America maintainers of GNU Linux-libre to ensure a fully free software kernel was installed on interested Fedora Linux systems and that no non-free packages were installed on the system. But now that effort has come to an end...

Microsoft Begins Providing DirectX Shader Compiler Linux Binaries

Phoronix - Sun, 12/18/2022 - 21:33
In early 2017 Microsoft open-sourced their DirectX shader compiler and shortly thereafter it's been possible to build it on Linux while finally as of this week Microsoft has begun providing official Linux binaries of their shader compiler...

SquashFS Gains Support For IDMAPPED Mounts With Linux 6.2

Phoronix - Sun, 12/18/2022 - 20:09
Introduced nearly two years ago with Linux 5.12 was IDMAPPED mounts for many innovative use-cases from containers to systemd-homed. With the Linux 6.2 kernel, SquashFS is the latest file-system adding support for IDMAPPED mounts...

NVIDIA-VAAPI-Driver 0.0.8 Introduces New Direct Backend

Phoronix - Sun, 12/18/2022 - 19:53
Introduced at the start of the year was an experimental open-source project implementing the VA-API interface over NVIDIA's NVDEC video decoding API. In turn this VA-API support for running atop NVIDIA's proprietary Linux graphics driver allows for GPU video acceleration within Firefox and other software only targeting the Video Acceleration API. Now in closing out the year is a new NVIDIA-VAAPI-Driver release...

Landlock Security Module Adds File Truncation Support With Linux 6.2

Phoronix - Sun, 12/18/2022 - 19:16
Merged back in Linux 5.13 last year was Landlock for allowing unprivileged application sandboxing. Landlock allows restricting ambient rights for a set of processes and is implemented as a stackable Linux security module (LSM) for establishing safe security sandboxes. With Linux 6.2 file truncation support is added for Landlock...

Linux 6.2 Introduces Several More Touchscreen Drivers

Phoronix - Sun, 12/18/2022 - 18:41
In addition to the HID driver updates for the Linux 6.2 kernel that were merged this week, the input subsystem updates also landed this week and were headlined by having several new touchscreen drivers...

Pages