Skip to content

ffmpeg Usage

wagwan piffting edited this page Mar 22, 2024 · 2 revisions

ffmpeg is a great command line recorder for this kind of task, so let's go into a little more depth on how to use it to record whatever you want.


If you're using a capture card:

Let's assume for the purposes of this portion of the guide you have a capture card. This can be any kind of capture card that ffmpeg recognizes and will record from. We need to get some information first about the capture card you have, so we can set it up for use with the batch scripts. First and foremost, we need to find the name assigned to it by ffmpeg. So, to do that, we need to first run the following command and pay close attention to the output:

ffmpeg -i dummy -devices

Now, in my case, I get the following output using the ffmpeg binary from Chocolatey:

Devices:
 D. = Demuxing supported
 .E = Muxing supported
 --
 D  dshow           DirectShow capture
 D  gdigrab         GDI API Windows frame grabber
 D  lavfi           Libavfilter virtual input device
  E sdl,sdl2        SDL2 output device
 D  vfwcap          VfW video capture

We can see here that "DirectShow capture" is available to us, 99% of the time this is what you'll want/need for a capture card. So, next, run this command and again, pay close attention to the output:

ffmpeg -list_devices true -f dshow -i dummy

Now I can see the following dshow devices on my system, this will vary depending on what kind of capture card you have and other factors. If you don't see anything listed here, try the other device methods (gdigrab, lavfi, etc.)!

[dshow @ 000001f9c6fab240] "Game Capture HD60 S" (video)
[dshow @ 000001f9c6fab240]   Alternative name "@device_pnp_\\?\usb#vid_0fd9&pid_005e#06146d81c1#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\{6f814be9-9af6-43cf-9249-c0340100021f}"
[dshow @ 000001f9c6fab240] "OBS Virtual Camera" (none)
[dshow @ 000001f9c6fab240]   Alternative name "@device_sw_{860BB310-5D01-11D0-BD3B-00A0C911CE86}\{A3FCE0F5-3493-419F-958A-ABA1250EC20B}"
[dshow @ 000001f9c6fab240] "Elgato Screen Link" (audio, video)
[dshow @ 000001f9c6fab240]   Alternative name "@device_sw_{860BB310-5D01-11D0-BD3B-00A0C911CE86}\{D2F41684-D46F-440B-8096-4FCD528ED5A3}"
[dshow @ 000001f9c6fab240] "Game Capture HD60 S Audio" (audio)
[dshow @ 000001f9c6fab240]   Alternative name "@device_pnp_\\?\usb#vid_0fd9&pid_005e#06146d81c1#{33d9a762-90c8-11d0-bd43-00a0c911ce86}\{6f814be9-9af6-43cf-9249-c0340100031f}"

So, we can see I have two capture card-related devices, both named "Game Capture HD60 S" and "Game Capture HD60 S Audio" respectively. This is the capture device I want to use and have set up to monitor a local television station using a cheap Ematic Tuner/DVR. Now, before we continue, SAMEDec needs the proper sample rate of the device we're using so it can demodulate the output properly. Let's find out the sample rate by running this command:

ffmpeg -f dshow -i audio="Game Capture HD60 S Audio" dummy

When I do so, I see the following output:

[aist#0:0/pcm_s16le @ 00000233bb750040] Guessed Channel Layout: stereo
Input #0, dshow, from 'audio=Game Capture HD60 S Audio':
  Duration: N/A, start: 131138.608048, bitrate: 1536 kb/s
  Stream #0:0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
[AVFormatContext @ 00000233bb7501c0] Unable to choose an output format for 'dummy'; use a standard extension for the filename or specify the format manually.
[out#0 @ 00000233bb754100] Error initializing the muxer for dummy: Invalid argument
Error opening output file dummy.
Error opening output files: Invalid argument

Now, ignoring the obvious errors at the bottom, we can see the following line:

Stream #0:0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s

In this line, "48000 Hz" is the sample rate and the number we need. So, I would then go to the main.bat and record_and_send.bat files and edit the names of the devices like so:

main.bat

(...snip...)

ffmpeg -f dshow -i audio="MY AUDIO DEVICE NAME HERE" -f wav pipe:1 | samedec -r SAMPLE_RATE_NUMBER -- %~dp0\record_and_send.bat

record_and_send.bat

(...snip...)

start ffmpeg_wrap.exe ffmpeg -f dshow -i video="MY VIDEO DEVICE NAME HERE":audio="MY AUDIO DEVICE NAME HERE" -bufsize 2G -framerate 60 -video_size 1920x1080 -map 0 -map 0:a -c:v libx264 -c:a aac C:\EAS_Alerts\%YYYYMMDD%-%random%%random%.mp4

(...snip...)

Now, let's break down both files and what they're doing exactly. main.bat is running the listener, SAMEDec, and will call record_and_send.bat at the end when it hears an alert. So, we edit "MY AUDIO DEVICE NAME HERE" to "Game Capture HD60 S Audio" and "SAMPLE_RATE_NUMBER" to "48000" to match the sample rate we saw earlier. Then, going to record_and_send.bat, we see a much longer command, so here's the breakdown of it:

  • start

    • This spawns the program into its own process so we can control it with much more granularity.
  • ffmpeg_wrap.exe

    • ffmpeg-wrapper is a wrapper file to help us trigger the closing of the recording safely without it getting corrupted. This exe comes bundled with this particular repository, and has been slightly modified specifically for the Game Capture HD60 S I have personally. If you wish to have a pre-compiled binary without these modifications, you can find it under ffmpeg_wrap_noedit.exe. PLEASE NOTE: There is a slight possibility that NEITHER EXE will work in your particular environment. Please be sure to test them and, if absolutely necessary, view and edit ffmpeg_wrap.nim in this repository for how I modified the program to work with my gear. YOU HAVE BEEN WARNED.
  • ffmpeg -f dshow -i video="MY VIDEO DEVICE NAME HERE":audio="MY AUDIO DEVICE NAME HERE"

    • This sets up the basic input of the capture card.
  • -bufsize 2G -framerate 60 -video_size 1920x1080 -map 0 -map 0:a -c:v libx264 -c:a aac

    • These are some various options I found while browsing the documentation of ffmpeg and what works for recording a normal .mp4 file for my gear. You may have to tweak, or remove, these options when setting up your own gear.
  • C:\EAS_Alerts\%YYYYMMDD%-%random%%random%.mp4

    • This tells ffmpeg where to put the recordings. This will store them in a folder in the root of the C drive in Windows. %YYYYMMDD% is the current date in year-month-day format (all numeric) and %random%%random% adds some random numbers in case there are multiple alerts that day so the first recording does not get overwritten.

And that's it! Assuming you have made the correct modifications to everything and you added your Discord webhook URL to record_and_send.bat, you should be ready to start receiving and relaying EAS alerts.


If you're using an audio stream:

Say you have available to you an online audio stream, such as that of your local NWS office's NOAA Weather Radio, or any other radio station really (so long as they have a standard format stream over the Internet). You would then modify the commands like so, making sure to set the SAMPLE_RATE_NUMBER properly (see above section for how to do that):

main.bat

(...snip...)

ffmpeg -i http://example.com/audio.ext -f wav pipe:1 | samedec -r SAMPLE_RATE_NUMBER -- %~dp0\record_and_send.bat

record_and_send.bat

(...snip...)

start ffmpeg_wrap.exe ffmpeg -i http://example.com/audio.ext C:\EAS_Alerts\%YYYYMMDD%-%random%%random%.mp3

(...snip...)

...with http://example.com/audio.ext being the link to your stream of choice. That's it!

Clone this wiki locally