2

I have a music directory on a debian computer, which from time to time gets too big files in it. To help me with eventual deleting of these files, I've installed mediainfo and made a script, that should go through all the files with in music directory using that command.

I'm trying to utilize the duration parameter to define what needs to deleted or not. The example input is:

mediainfo --Inform="General;%Duration%" /home/administrator/music/Example\ Full\ OST.mp4
7838987

Output returns duration as milliseconds. Please note, that if files have any spaces in them, mediainfo marks a backslash in front of them. I've taken this into account in my script:

#!/bin/bash
for i in /home/administrator/music/*
do
# Changing i to readable form for mediainfo
i=$(echo $i | sed -r 's/[ ^]+/\\&/g')
echo $i
# Go Through files, storing the output to mediadur variable
mediadur=$(mediainfo --Inform="General;%Duration%" $i);
echo $mediadur;
done

echo outputs are:

/home/administrator/music/Example\ Full\ OST.mp4
 

The mediadur echo output doesn't show anything. But when I copy the first echo output to the first example, it shows same output.

However, if the directory has any media that doesn't have space in its filename, the script works fine. The output of the script:

/home/administrator/music/546721638279.mp3
83017

This problem has left me very puzzled. Any help is appreciated.

TukeDuke
  • 21
  • 2

2 Answers2

2

You should updates this line:

mediadur=$(mediainfo --Inform="General;%Duration%" "$i");

Double quotes will prevent globbing and word splitting

Samuel
  • 3,631
  • 5
  • 37
  • 71
1

Actually this is not related to MediaInfo, just that you provide a wrong file name to the command line. And MediaInfo adds no "backslash in front of them".

Your escape method does not work as you expect.

#!/bin/bash
for i in music/*
do
    # Go Through files, storing the output to mediadur variable
    mediadur=$(mediainfo --Inform="General;%Duration%" "$i");
    echo $mediadur;
done

Works well also with file names having spaces.

Jérôme Martinez
  • 1,118
  • 5
  • 10