kiophen

Linux Tips

After having had a lot of time to get used to Linux (and importantly, the terminal), I realize that there are some things that can only be done with the terminal. These are situations where I would otherwise have to use an online tool, do some tedious manual work, or it's just something that's only really possible with the terminal.

I will update this as I remember more tips. I've been using Linux for long enough that I won't be able to think of everything Linux-specific I do, it's just normal to me now.

Some of these will be things that I would only have to do once per installation, so I can remember how I did them in the future.

If you don't use Linux, you can still look through this to see what kinds of things you can do with the terminal.


Routing Audio

There are a few applications for this, but my case is just this: I want OBS to only record certain program's audio, while ignoring all other programs. I want to be able to stream games while in a call and not broadcast me & my friend's voices.

On Windows, I think OBS has built-in support to just grab a program's audio now. It used to not be the case, and you'd have to use a program like Virtual Audio Cables to do this. On Linux, this is not supported yet, so I have to run some commands in the terminal.

pacmd load-module module-null-sink sink_name=game_sink sink_properties=device.description=Game-Sink

The above command makes a new "audio sink" called Game-Sink with pulseaudio. I am not going to explain pulseaudio, no one knows how it works. I found this command on the internet.

Game-Sink will be our virtual audio device that OBS will be able to record audio from. I can switch a program's audio device using my volume control settings. However, the audio won't be outputting through my headphones, since that's a different device. I can solve this by making my headphones the monitor device for Game-Sink.

pacmd list-sinks | grep name:

This command lists out the audio sinks. The output that I get is below, yours will be different.

name: <alsa_output.pci-0000_0e_00.1.hdmi-stereo-extra2>

name: <alsa_output.pci-0000_10_00.4.analog-stereo>

name: <game_sink>

I see my HDMI screen's speaker and an analog-stereo sink, along with the Game-Sink I just made. My headphones are the analog-stereo, so I'll take that whole string of text inside the arrow brackets and use it in the next command:

pacmd load-module module-loopback source="game_sink.monitor" sink="HEADPHONES SINK NAME HERE"

Now I can set a program's audio device to Game-Sink through my system volume control, and then make an audio device capture in OBS that captures Game-Sink.

This will only last until you turn your computer off. To make it permanent, I have to make a file in /etc/pulse/default.pa.d/ (create the default.pa.d folder if it doesn't exist) called anything.pa and paste the above commands, excluding the "pacmd". You will need sudo permissions to mess with files in /etc/.


Downloading Youtube/Twitter Videos

You know when you want to download a song from a youtube video and then you spend a couple minutes trying whatever "youtube to mp3" result shows up on google? and then the first one doesn't work so you have to try the second result? and then that one takes an absurd amount of time to work? and then the next one asks you to type in your email address and they say they'll email you the mp3 in 1-2 business days

To permanently avoid this, I use a package called yt-dlp. You can find it in your package manager or using apt-get.

It has a terrifying amount of options, but is also simple if you want it to be. To download a video, you just open your terminal in the folder you want to download the video to and type the following:

yt-dlp "VideoUrlGoesHere"

It will download the best quality video file available directly to your computer.

If you just want the mp3, then just add this:

yt-dlp "VideoUrlGoesHere" -x --audio-format mp3

"-x" is the flag to eXtract the audio, and "--audio-format mp3" forces the audio to be mp3. Otherwise you could get some weird file formats.

*IMPORTANT! You can replace the video url with a *youtube playlist url* and it will download everything in the playlist. You can also replace it with a tweet URL to download the video in the tweet. It probably works with other websites as well.

Insane Specificity

There are a lot of other flags you can add to make yt-dlp download videos in the most specific ways beyond your comprehension:

yt-dlp "UrlGoesHere" -f "bestaudio+bestvideo[width<=360]" -w --max-filesize "15M" --remux-video "mp4"

Here's the command I use to download the videos for my gelplayer on my homepage here. It will download a video with the best audio available, with a video-quality of only 360p or lower. It will also refuse to download if the file ends up being over 15mb, and then makes sure it's an mp4 in the end. The -w makes it not overwrite any existing files.


List Files In A Folder

ls foldername > dir.txt

This will make a file called dir.txt that has all the filenames of files in the chosen folder, one filename per line.

I used this for making lists of files for my gelmusic player and gelsoft gallery page. I'm not sure why else you would want to do this other than for programming projects.