0

I have a large number of downloaded radio programs that consist of 4 mp3 files each. The files are named like so:

Show Name - Nov 28 2011 - Hour 1.mp3

Show Name - Nov 28 2011 - Hour 2.mp3

Show Name - Nov 28 2011 - Hour 3.mp3

Show Name - Nov 28 2011 - Hour 4.mp3

Show Name - Nov 29 2011 - Hour 1.mp3

Show Name - Nov 29 2011 - Hour 2.mp3

Show Name - Nov 29 2011 - Hour 3.mp3

Show Name - Nov 29 2011 - Hour 4.mp3

Show Name - Nov 30 2011 - Hour 1.mp3 and so on...

I have used the cat command to join the files with great success by moving the four files of the same date into a folder and using the wildcard:

cat *.mp3 > example.mp3

The files are all the same bitrate, sampling rate, etc. What I would like to do is run a script that will look at the file name and combine hours 1-4 of each date and name the file accordingly. Just the show name, the date and drop the 'Hour 1'.

I looked around and found a number of scripts that can be used to move files around based on their names but I'm not adept enough at bash scripting to be able to understand the methods used and adapt them to my needs.

I'm using Ubuntu 14.04.

Many thanks in advance

cainram
  • 11
  • 3
  • 2
    Are you aware that binary file formats are very different from plain text? I'm sure you're just generating a huge corrupted file and you don't notice because your MP3 player is smart enough to fix the damage on playback. – Álvaro González Jul 22 '14 at 15:33
  • I looked into that a little bit and found some back and forth on it. These are just for my personal use and I'm not too worried about it. If this were a production scenario I would use some other command that would re-encode the files, etc. Thanks for the heads up, though. – cainram Jul 22 '14 at 15:43

1 Answers1

0

You can use a bash for loop to find each distinct date name and then construct the expected mp3 names from that.

Because your files have spaces in their names and my solution uses globbing, you'll also have to edit your Internal Field Separator to ignore spaces for the duration of the script.

SAVEIFS=$IFS
IFS=$'\n\b'
for mdy in `/bin/ls *mp3 | cut -d' ' -f'4,5,6' | sort -u`; do
    cat *${mdy}*.mp3 > "showName_${mdy}_full.mp3"
done
IFS=$SAVEIFS

This won't alert you if some hours are missing for some particular date. It'll just join together whatever's there for that date.

Note: The comment pointing out that cat probably won't work for these files is spot on. The resulting file will probably be corrupted. You probably want to use something like mencoder or ffmpeg instead. (Check out this thread.)

Community
  • 1
  • 1
dg99
  • 5,456
  • 3
  • 37
  • 49
  • I put the code into a script and called the file 'testscript.sh'. When I execute from the command line I get the following output: cat: *-*.mp3: No such file or directory cat: *Dec*.mp3: No such file or directory cat: *21*.mp3: No such file or directory cat: *-*.mp3: No such file or directory cat: *Dec*.mp3: No such file or directory cat: *22*.mp3: No such file or directory cat: *-*.mp3: No such file or directory cat: *Dec*.mp3: No such file or directory cat: *23*.mp3: No such file or directory cat: *-*.mp3: No such file or directory cat: *Dec*.mp3: No such file or directory ---etc. – cainram Jul 22 '14 at 15:56
  • I also get 13 files with 0 bytes named: /home/cainram/Desktop/2009-12/showName_21_full.mp3 showName_22_full.mp3 showName_23_full.mp3 showName_24_full.mp3 showName_25_full.mp3 showName_26_full.mp3 showName_27_full.mp3 showName_28_full.mp3 showName_29_full.mp3 showName_30_full.mp3 showName_31_full.mp3 showName_Dec_full.mp3 showName_-_full.mp3 – cainram Jul 22 '14 at 15:58
  • Good point. There are spaces in your file names, so I edited my script. – dg99 Jul 22 '14 at 16:43
  • I now get a file (0bytes) called: showName_- Dec 21 - Dec 22 - Dec 23 - Dec 24 - Dec 25 - Dec 26 - Dec 27 - Dec 28 - Dec 29 - Dec 30 - Dec 31_full – cainram Jul 22 '14 at 16:55
  • I could use a batch renamer to remove the spaces from the files or replace them with - if that would help... – cainram Jul 22 '14 at 16:56
  • I really appreciate the help, by the way. – cainram Jul 22 '14 at 16:57
  • What is the result of this command? `/bin/ls *mp3 | cut -d' ' -f'4,5,6' | sort -u` – dg99 Jul 22 '14 at 16:58
  • - Dec 21 - Dec 22 - Dec 23 this goes on down to - Dec 31 These are the only dates I have shows for in this folder. – cainram Jul 22 '14 at 17:08
  • Ah, so "show name" must actually consist of three words, not two. That would cause the `cut` command to pull the wrong fields. You need to adjust the fields selected in the `cut -f` command so they match the month, day, and year of the show. (Hence `mdy` as the variable name.) – dg99 Jul 22 '14 at 17:10
  • Ok, now the output shows Dec 22 2009 and on and on. I'm still getting an MP3 file with all of the dates in the name and 0 content. – cainram Jul 22 '14 at 17:13
  • The file name is showName_Dec 21 2009 Dec 22 2009 Dec 23 2009 Dec 24 2009 Dec 25 2009 Dec 26 2009 Dec 27 2009 Dec 28 2009 Dec 29 2009 Dec 30 2009 Dec 31 2009_full.mp3 – cainram Jul 22 '14 at 17:21