1

Is there a way to find and delete all the audio files recursively (MP3 files) which have less than 3 minutes play duration time?

Consider the situation that I have mixture of multiple format files (e.g. directories, text files and mp3 files).

  • Updated my question @don_crissti – smc Oct 19 '18 at 19:20
2

Here's one way. Run mediainfo against every mp3 file, if shorter than 3 minutes, delete it.

#!/bin/bash
for FILE in $(find . -type f -name \*.mp3); do
    [[ $(mediainfo --Output='Audio;%Duration%' "${FILE}") -lt "180000" ]] && rm "${FILE}"
done

Or for fans of one-liners:

find . -type f -name \*.mp3 -exec bash -c '[[ $(mediainfo --Output="Audio;%Duration%" $1) -lt "180000" ]] && rm "$1"' -- {} \;
3

You'll need to cobble together a shell script as @dirkt mentions in his answer.

You can use ffprobe from the ffmpeg group to get duration in seconds -

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1  /path/to/mp3/file.mp3

You can use find to find all files ending in .mp3 in a given directory and any/all subdirectories, and call a script providing the path/filename of any files found

find /search/from/dir -type f -iname "*.mp3" -exec /path/to/delete_if_short.sh {} \;

Create your delete_if_short.sh script - use the ffprobe command to check length, if under 180 (value is in seconds, so 3 minutes) then rm the file and you are good to go.

2

There are a number of tools which can print out the play duration time of various audio file formats, e.g. sox, mediainfo and others. Which tool to use depends on the format of your audio files, which you didn't tell us.

You can process this output with grep etc., and use it in a shell script inside a loop as a condition whether to delete the file.

  • OP did say "(MP3 files)" – number9 Oct 19 '18 at 21:14
  • 1
    @number9 - no, that was after my comment (which was after dirkt's answer...) – don_crissti Oct 19 '18 at 21:15
  • 1
    Ah, sorry about that. – number9 Oct 19 '18 at 21:25
1

For some reason my find-foo was not up to par, so I hacked one stackexchange answer of a find replacement and came up with this.

#!/bin/bash
# mytime is the number of seconds of the mp3 that you want to delete,
# in this case 3 minutes
mytime=180
files="$(find -L "<put your top level directory here>" -type f -name  "*.mp3")";
# are there any files at all?
if [[ "$files" == "" ]]; then
    echo "No files";
    return 0;
fi
echo "$files" | while read file; do 
    # take the file, find the time, convert to seconds
    times="$(mp3info -p "%m:%s\n" "$file" |awk -F':' '{print ($1*60)+$2}')"
    # if that is greater than 3*60, we delete the file, which is $file.
    if [[ "$times" -lt "mytime" ]]
    then
        # WARNING, there be dragons here... 
        echo "We are removing $file from the system..."
        rm "$file"
    fi 
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.