DEV Community

Cover image for How to record webcam video and audio using ffmpeg
Ethan
Ethan

Posted on

13 1

How to record webcam video and audio using ffmpeg

Introduction

Hello! ๐Ÿ‘‹

In this tutorial I will show you how to use the ffmpeg command to record your webcams video and audio to an mp4 file.


The Command

The command is a pretty simple one liner. (Please note my machine is Linux so the format/input may change depending on your OS)
You can alter the command based on your webcam input by checking the following documentation:
https://trac.ffmpeg.org/wiki/Capture/Webcam



ffmpeg -f pulse -ac 2 -i default -f v4l2 -i /dev/video0 -t 00:00:20 -vcodec libx264 record.mp4


Enter fullscreen mode Exit fullscreen mode

The -f defines the format, pulse for audio and v4l2(Video 4 Linux 2).
The -ac defines the audio channels, in this case 2.
The -i defines the input, default for audio and the webcam for video.
The -t defines the duration of the recording, in this case 20 seconds.
The -vcodec defines the output video codec, since it is an mp4 file it is set to libx264 (H.264)
Audio should default to AAC so -acodec is not needed.


Conclusion

Here I've given a very simple demonstration on how to use the ffmpeg command to capture the user's webcam footage with audio.

I'm thinking of creating a C++ example and will let you know when that's finished. ๐Ÿ˜Ž

Side note, I'm sorry for the wait on my WebRTC Android Tutorial, been pretty hectic recently but it's almost finished!

Hope you found this quick tutorial useful and if you have any useful resources for learning FFmpeg please share them with me.


Like me work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.

โ€œBuy Me A Coffeeโ€

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs