What a script of 6 lines of code can teach you — from 5 min to seconds

Hector Herradura
UX Collective
Published in
5 min readSep 2, 2019

--

Intro

So this is the case, my car an Alfa Romeo GT only plays 192kbps bitrate mp3 through an USB (other bitrates provoke artefacts at the speakers), and to play music first I must convert it to this specific file type. This was resulting in an tedious process where I was spending an average of 5 min to 10 min, to open the converter, setting the files, specifying the bitrate and then waiting to pass my music to the USB.

I love music and I was getting boring due to my music hadn’t changed so much since a year or two because I had to spent time doing conversions. So I wanted to create something fast and an automatic, like a Stradale.

Alfa Romeo 33 Stradale designed by Franco Scaglione, 1967

Choosing tools and programming languages.

The first tool I needed was the converter something that would change all my music, the tool in this case is FFmpeg which is a command line tool that can convert, record and stream audio and video.

The next step was to create the script, at this time I was deciding between DOS, Python and PowerShell.

  • I didn’t want to spent much time learning a new language so Python was discarded.
  • I was very tempted to use PowerShell but a quick search on internet taught me that PowerShell doesn’t admit drag and drop files for security reasons, this was a big no.
  • So the decision was taken, to program it the arcane DOS language which I have very little experience with and to convert it later into a batch file.

The program

The program itself is very simple, a loop with a parameter that takes the value of files being dropped, that passes the file-path to be converted to FFmpeg, at the end of the loop shift the parameter to next one. That’s all. Thank you to FFmpeg I can even pass video to it.

:again
set
inputFile= %1
setlocal EnableDelayedExpansion
set directory=J:\Music\Car\
set fileToMP3= "%directory%%~n1.mp3"
if [%1]==[] goto:end
ffmpeg -i %inputFile% -vn -ar 44100 -ac 2 -b:a 192k -f mp3n %fileToMP3%
shift
goto again
EXIT /B 0
:end
echo Conversion completed. Enjoy it and outrun.
pause
Exit

Of course it will not be a completed Batch file if I didn’t include some ASCII art that I could use as an indicator. I tried to use some image to ASCII online tools but they weren’t working as expected so I decided to do it on my own.

The reference is a piece of art designed by Marcello Gandini in 1968, an Alfa Romeo Carabo.

Alfa Romeo Carabo designed by Marcello Gandini (1968)

And this was the result.

Alfa Romeo Carabo ASCII art by Héctor Herradura

What I learned

  • Spaces and special characters can broke your program if you are dealing with old languages: I have always look after my file names when working in web design, to not to have spaces but my music folder is a chaos because of special characters like [,( and spaces. This was throwing me errors and provoking my CMD to close suddenly. I managed to solve this using double quotes on my variables. I was aware of this, but this was first time I had a real problem when programming with spaces and because some quirks of FFmpeg.
  • When looking for solutions you have to pay attention to version you are using: specially if you are using old languages with different implementation in different OS like DOS. I was always finding solutions to new versions but no mine, it took me a while to find one that fit my version.
  • Every program has a quirk side where things start to get complicated if you don’t know the specific behaviour is causing the error. You can spend a lot of time playing with code to find the right solution.
  • When you start to mix programs you start restraining your options. To mix two or more programs can increase complexity due to each one can be treating the same variable in different ways, causing you to add more lines of code or to break your program.
  • Every program needs Indicators, even the CLI programs. The third time I ran my script I realised I didn’t know if the conversion was done or if the file was corrupted or if there were an error. So I added an indicator, to tell me the conversion have been done successfully, in this case I created a ASCII art and forced the window to not to close after the process ended.
  • ASCII art is very complex, it’s like painting with ink, an error on the proportions and you have to start again, in the other hand I found it very absorbing, you should try it.
  • Automation requires time, but then when you got it all time invested is worthy. I spent like three hours because I was not familiarized with DOS syntax but now instead of spending 5 min each time that I want to convert a song I spent a few seconds and I enjoy new songs every week.
  • You will miss a debugger tool. If you have a debugger tool included in the system you are using to write your program you can feel safe, if not, you are doomed. I was really missing a tool like JavaScript debugger tool included in browsers, while they can show you the value a variable is taking and what’s going on they also make you to learn faster.

--

--

The computer an extension of the human intellect, programmed by master control to survive by all means. Designer. https://www.linkedin.com/in/hector-herradura