0

I have approximately 60 audio files with a partial time stamp in their names. These names were created automatically by the recording system and are shown as below

201106_0099.wav
201106_0100.wav
201106_0101.wav
.
.
.
201106_0163.wav

The files are in a folder named with the date (yymmdd), initial and ending hour, minute and second (HHMMSS) of the recordings just like

Audios_201106_063044_190708

Each audio file has exactly 11 minutes 38 seconds.

Using the command stat from terminal with zsh I get, for the first file, I get information about the start time of a recording:

my_name@Air-of-My_Name Audios_201106_063044_190708 % stat 201106_0099.wav
16777221 1381621 -rw-rw-r-- 1 my_name staff 0 67032620 "Mar  4 21:11:14 2021" "Nov  6 06:30:44 2020" "Mar  4 12:06:13 2021" "Nov  6 06:30:44 2020" 4096 130928 0 201106_0099.wav

Using ffmpeg, I get info about the duration of the recording:


my_name@Air-of-My_Name Audios_201106_063044_190708 % ffmpeg -i 201106_0099.wav
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.29)
  Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from '201106_0099.wav':
  Duration: 00:11:38.26, bitrate: 768 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 48000 Hz, mono, s16, 768 kb/s

Almost the same info I get from SoX


my_name@Air-of-My_Name Audios_201106_063044_190708 % soxi 201106_0099.wav     

Input File     : '201106_0099.wav'
Channels       : 1
Sample Rate    : 48000
Precision      : 16-bit
Duration       : 00:11:38.26 = 33516288 samples ~ 52369.2 CDDA sectors
File Size      : 67.0M
Bit Rate       : 768k
Sample Encoding: 16-bit Signed Integer PCM

I would like to batch rename all of them with their complete time stamp, just like

0099_yyyymmdd-HHMMSS.wav.

What would be a good, maybe 'swiss army knife' solution to my problem, using unix, SoX or even Python? Thanks in anticipation.

  • Each line of information can be stored in a variable. Then extract each item of data you need, and rebuilt your new filename with these items. This can be done in bash, or any other language that you have available. – Nic3500 Mar 05 '21 at 02:24
  • Do you want a literal `0099_` prefix for all of your files? Do you want your file names to have any information on the duration of the recording, or do you only want to have the timestamp of the file's creation time? – Shane Bishop Mar 05 '21 at 20:51
  • @ShaneBishop the file name with a prefix is just an example. The most important is the timestamp of the file's creation time in each file of the folder. – Mario Rollo Mar 06 '21 at 16:47
  • @Nic3500 Unfortunately, I am not any good with managing variables in an unix environment. – Mario Rollo Mar 06 '21 at 16:49

1 Answers1

0

You can format a file's creation time to match your yyyymmdd-HHMMSS format using date and stat:

date -d "@$(stat -c '%W' /path/to/file)" '+%Y%m%d-%H%M%S'

stat -c '%W' tells stat to display the time of file birth in seconds since the Unix Epoch (1970-01-01 UTC).

If the argument to date's -d option is prefixed with a @, then the value will be interpreted as seconds since the Unix Epoch.

You could use the following shell script to batch rename all of the .wav files:

# Put the correct path here
cd /path/to/Audios_201106_063044_190708 || exit 1

for wav_file in *.wav; do
    # Get new name
    new_name="$(date -d "@$(stat -c '%W' "./$wav_file")" '+%Y%m%d-%H%M%S')"
    
    # Rename file to new name
    mv "./$wav_file" "./$new_name"
done
Shane Bishop
  • 3,905
  • 4
  • 17
  • 47