Using my Nikon D3300 as a webcam

Nikon D3300 as a webcam on linux

Recently, I've been doing a lot more YouTube videos. As I've been getting more advanced in my video production work, I realized that I really needed to up my video quality. I already had a Nikon D3300, but I didn't know how to make it work as a webcam. When I first looked it up, everything I found suggested getting an expensive piece of hardware to capture the HDMI feed and send it to the computer, but I could tell that there was a USB connection port and I suspected that there was a cheaper way to connect my camera to my linux laptop using USB and open source software.

Sure enough, there is a solution that didn't cost a lot and that uses open source software to connect everything up.

First, I did need a couple of hardware things to make it all work.

I think that the USB chord was supposed to have come with the camera, but I got the camera second hand as part of a trade, so I never had that cable. If don't have the cable or can't find yours in your pile of random cables, this replacement will do the trick:rgba(0, 0, 0, 0.05)

Next, if you are going to be using your camera for a long time, your battery is going to run out. Having to swap batteries while you are making a video is a pain, so don't. Instead, just use an adapter that will connect the camera directly to mains electricity:

These two things together will run you about $40 to $50 instead of $150 - $300 for an HDMI capture device. And, of course, even with the HDMI capture device, you'll still need to deal with the power problem. 

Alright, now that you have your connection and power source handled, you are ready to do the software side of things.

There are three pieces of software that you absolutely need, and one that's a nice to have.

gphoto2 is the software that allows your computer to connect and operate your camera from your linux machine.

v4l2loopback-utils will allow that camera to be available to other pieces of software, like ffmpeg. 

ffmpeg is what you are going to use to create a virtual device out of the feed from your camera.

entagle isn't necessary for the webcam set up, but it's useful for troubleshooting. It's main use is to manage the camera from your computer, using gphoto2's api. 

 

How to install:

Debian/Ubuntu

$ sudo apt-get install gphoto2 v4l2loopback-utils ffmpeg entangle

 

Arch Linux

$ pacman -S gphoto2 v4l-utils ffmpegntangle

 

Fedora

$ sudo dnf install gphoto2 v4l2loopback ffmpeg entangle

 

 

Making it all work together

In order to make this all work, you are going to have to lode the kernel module. This time, we'll do it manually, like this:

$ sudo modprobe v4l2loopback exclusive_caps=1 max_buffers=2

But for the future, let's just make that happen automatically on boot.

Use sudo to create a new module config file called /etc/modprobe.d/dslr-webcam.conf with this content:

# Module options for Video4Linux, needed for our DSLR Webcam alias dslr-webcam v4l2loopback options v4l2loopback exclusive_caps=1 max_buffers=2

And use sudo to edit the /etc/modules file, and add the line

dslr-webcam

to the end of the file.

And now you are ready for the magic.

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

Once you've done that, you are ready to use that video in other programs like OBS for video streaming or Google Meet for videoconferencing. You'll find your camera listed as "dummy video".

Dummy video device (0x0000)

 

NB: If, like me, you already figured out that your camera is sitting at video1 and not video0, you might be tempted to change the above line to match where you know your camera really is. I did that. And then I spent hours wondering why nothing would work.

Another Note: Every time I restart my computer or plug my computer in a new, the computer mounts the camera as a filesystem because it really, really wants to read the images on the SD card instead of letting me use the camera directly. Unfortunately, when the camera is mounted as a filesystem, it won't work as a camera. Unmount the camera filesystem, and all will be right with the world.

 

One last thing...

I don't know about you, but I'm never going to remember that long command to get my camera going. I'd rather use a short command like "startcam". So, we'll make an alias in our ~/.bashrc file. Use your favorite editor, and pop on down to the bottom of that file to add these lines:

# start the DSLR camera running so it can be picked up by other programs
alias startcam="gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0"

Once you save that file, you'll want to reload your .bashrc with this command

$ source ~/.bashrc

And now you can just type startcam to start your camera and ctrl-c to stop it. 

Have fun!

Section