Thursday, November 25, 2021

ffmpeg-convert.sh

i recently came across the need to quickly rip audio track from video files. my first thought was to use ffmpeg and wrap it in a script. sometimes i may have one or 20 MP4/MKV/webm files that need to be converted to an MP3. the files may be scattered across a few directories, but each serves its own purpose. thus, i wrote a script to automate a lot of repetitive work.

the script, called ffmpeg-convert.sh, makes a lot of assumptions and works fine as an underprivileged user. it expects the files to be actual video files and will fail gracefully if the file doesn't exist or ffmpeg unexpectedly exits. it will continue where it left off if you CTRL+C for some reason. it also has a neat spinner so that you don't have to wonder if it's working or not. some large files can take a while to process.

i endeavored to write as much of the functionality as possible in BASH. the sub-string search and replace is kind of disgusting, but it does a fairly decent job of sanitizing the filename.

the script works like this: when invoked, it scans the current working directory for files with "known" video file extensions. it prompts you with how many files were found and asks for permission to continue. it then processes each file via ffmpeg and bakes you a fresh MP3 file. it's pretty simple, and i might consider adding more functionality to the script. i've already received a couple of requests!

grab the script from here.

 

 

 

Monday, November 8, 2021

git log query, or: glq

Sometimes you've got to take the time to build something so you can be more productive. Also, it never hurts to make life easier on yourself. I've found git log can be a source of zen when combined with disciplined git commit messages.

To give you some background, I have a wrapper around the git command that templates commit messages. It helps to ensure consistent, detailed, quality commit messages for posterity. 

The template uses the following format:

KEYWORD: path/to/file/relative/to/repo/file.ext; Commit message here. (Ticket serial number, if applicable)

The KEYWORD can be one of: NEW, CHANGE, REMOVED, OOB. Some files are stored in a directory named after the host they're installed on, and the ticket serial number can vary depending on how the work request was received. 

Here is an example of a real commit message:

CHANGE: files/ssl-certificates/my.host.com/my.host.com.pfx; Updated SSL Certificate and rotated private key. (TKT002048)

This allows me to search the git log for things like "NEW" files, the last "CHANGE" made to an SSL Certificate or firewall config, or when files were "REMOVED". This also helps me provide an easily searchable git audit log, should I ever need to, during a Compliance Audit or Incident Investigation.

However, using git log --grep "CHANGE.*my.host.com.pfx" --extended-regexp -n 1 got to be a bit cumbersome. I wanted a short command that could also take an option to increment "-n" as needed. A script felt like overkill for this situation, so I opted for a BASH function that's loaded into the shell session via .bashrc (on most GNU/Linux) or .bash_profile (on macOS). 

With this function, I've created a simple command: glq

Here is an example of how glq can quickly return the latest commit where the firewall configuration was changed for the host my.host.com:

$ glq "CHANGE.*my.host.com/firewall.yml"

Since git commits are "first come, first serve", a commit authored in Oct 1 could be merged on Nov 1 and would thus be considered the latest commit (or change). So, when thinking about the commit log in terms of, "descending, chronological order," we must think of it as the reverse (starting from now), chronological order in which commits were added/merged into your working branch.

By default, glq will only return the latest, first matching commit. If you want to expand the limit to 5, you can specify an integer after the double-quote encapsulated search query. 

For example:

$ glq "CHANGE.*my.host.com/firewall.yml" 5

The above example (building off the previous example) has "5" specified after the search query. This (essentially) tells glq/git to limit matches to the five most recent commits, instead of only the latest one. 

For even more fun, if I want to find the latest commit where a customer requested a change to a system, I could use the following search query:

$ glq "CHANGE.*customer.host.com/firewall.yml.*TKT"

Which would match the following commit message:

CHANGE: host_vars/customer.host.com/firewall.yml; Customer requested opening port 8443. (TKT004096)

As you can see, this has the potential to be a very powerful tool. It really only works, though, if you are consistent with your commit message formatting either through policy or template, or both.

Have fun!

Wednesday, June 23, 2021

The calendar broke (again). I fixed it (again).

Such is the life of anyone in IT - you fix something, it works for a while, then it inevitably breaks again.

This time it's different, and I don't plan on it breaking again, unless there are massive changes across the board to the back end.

This update is minor and I've retrofitted all previous downloadable files with the fix.

In short, I didn't read the man page completely and thus the %b option for date printed the abbreviated month. Well, May is only three letters long, so its full name is the same as its abbreviated name. Since Jun does not equal June, the regex match for the month fails. If we change the date command's formatting argument to %B, then we get the full month and the regex match completes.

I've updated the patch file (if you're still using v1) and v2 of the script.

Link: Script v2

Link: Patch file (if you're still on v1 and want to upgrade to v2) 

Have fun!

Thursday, May 20, 2021

A pretty, pretty cal(endar): Rev1

In my last blog post, I provided a BASH function that can operate as a lightweight replacement for cal. When I say "replacement", I mean, it just gives cal a bit of a facelift by making it pretty.

I'm here today to post a patch to that function. The regex got wonky when matching 20 or 21, so the header with the month and the year got dirty.


I also got tired of seeing the double-spaces at the end of the current week. That has only been an issue with the Apple version of cal. However, with some regex revisions, we arrive at a once again pretty, pretty cal(endar).

Somehow, my blog got marked as a malware-potential-phishing-don't-go-there-it's-Knockturn-Alley (please don't sue me, J.K.!) website because of my last post. To avoid this from happening again, I'm instead going to post links to the scripts and the patch (so you can see the diff).

 I have tested this out quite thoroughly on macOS and Linux, but do keep in mind that YMMV.

Again, standard attribution CC license applies to this code.


Link #1: v1 of the function

Link #2: v2 of the function

Link #2: The patch file

 

Until next time ...

Friday, May 14, 2021

A pretty, pretty cal(endar)

I live on the command-line and that means I need tools to keep me efficient. One tool that I use frequently is "cal". However, the default output is quite plain and (at times) is hard to read. So, I slapped together a shell function that colorizes the output and makes using it at-a-glance much easier.

The default output looks like this:


With colorized output, it now looks like this:

I always have a terminal session open with an active SSH connection to my Linux box, and another terminal session open for conducting business locally on my Mac. So, I needed this to work for both Mac and Linux. I learned the hard way that "echo -e" is really only useful on a Linux system. Thus, I have started using "printf" when I want formatted output beyond a simple "echo". I should probably revise this function to use "tput" and such, but for now I'm using ANSI escape codes for coloring text.

For macOS, you'll want to place this function in your ~/.bash_profile. On your Linux box, storing it in ~/.bashrc should be fine.

It probably goes without saying, this is provided to you without any warranty or guarantee, etc., etc., feel free to use or redistribute. Attribution would be nice, but not really necessary unless it's being included in something proprietary or a for-profit solution. Nonetheless, enjoy! 

 

Link #1: A pretty, pretty cal(endar) v1


Until next time!


Ref: https://misc.flogisoft.com/bash/tip_colors_and_formatting