Results 1 to 6 of 6

Thread: Bash script to recursively display duration of MP3 & MP4 files

  1. #1
    Join Date
    Dec 2005
    Beans
    Hidden!
    Distro
    Kubuntu

    Bash script to recursively display duration of MP3 & MP4 files

    There are some teaching sessions where the video duration/length is quite different to the audio lengths, and therefore I need to check the duration of all the videos and audios. I found this bash script to display the duration of MP3 files and simply modified it to display MP4 files as well ..

    Code:
    #!/bin/bash
    for file in *.mp3 *.mp4
    do
     duration=$(ffprobe "$file" 2>&1 | awk '/Duration/ { print $2 }')
     echo -e $duration"\t"$file
    done | sort -n
    and the output is ..

    00:29:06.67, Session 3 - Session3Audio.mp3
    00:29:30.60, Session 3 - Session3Video.mp4
    00:46:12.31, Session 2 - Session2Audio.mp3
    00:46:12.31, Session 4 - Session4Audio.mp3
    00:46:36.25, Session 2 - Session2Video.mp4
    00:54:45.74, Session 1 - Session1Audio.mp3
    00:55:09.70, Session 1 - Session1Video.mp4
    01:14:53.99, Session 4 - Session4Video.mp4
    but when I try the script in a higher level path/directory, where there are only folders (no files), the output is obviously an error because the script does not cater for going down all the paths recursively ..

    *.mp3
    *.mp4
    Can someone please tell me how to modify the bash script to make it do a recursive search. Also, I wanted to copy the output into a spreadsheet, so that the filename is first, then the duration. Is that simply a matter of adding a TAB character so that an import to the spreadsheet will be placed into columns.

  2. #2
    Join Date
    Jan 2017
    Beans
    235

    Re: Bash script to recursively display duration of MP3 & MP4 files

    Hi,


    Use the find command and pipe it to the bash builtin read.
    Code:
    #!/bin/bash
    find /root/dir -name "*.mp3" -o -name "*.mp4" | while read file;
    do
     duration=$(ffprobe "$file" 2>&1 | awk '/Duration/ { print $2 }')
     echo -e $duration"\t"$file
    done
    Quote Originally Posted by oygle View Post
    Also, I wanted to copy the output into a spreadsheet, so that the filename is first, then the duration.
    Perhaps I misunderstand, but won't exchanging the the fields in the echo command do what you want?
    Code:
    echo -e $file"\t"$duration

  3. #3
    Join Date
    Dec 2005
    Beans
    Hidden!
    Distro
    Kubuntu

    Re: Bash script to recursively display duration of MP3 & MP4 files

    Quote Originally Posted by norobro View Post
    Use the find command and pipe it to the bash builtin read.
    Code:
    #!/bin/bash
    find /root/dir -name "*.mp3" -o -name "*.mp4" | while read file;
    do
     duration=$(ffprobe "$file" 2>&1 | awk '/Duration/ { print $2 }')
     echo -e $duration"\t"$file
    done
    Great thanks, that works just fine, and goes down all the paths in a recursive manner.

    Quote Originally Posted by norobro View Post
    Perhaps I misunderstand, but won't exchanging the the fields in the echo command do what you want?
    Code:
    echo -e $file"\t"$duration
    Yes that did it, thanks. here is the final version ..

    Code:
    #!/bin/bash
    find /root/dir -name "*.mp3" -o -name "*.mp4" | while read file;
    do
     duration=$(ffprobe "$file" 2>&1 | awk '/Duration/ { print $2 }')
     echo -e $file"\t"$duration
    done | sort -n

  4. #4
    Join Date
    Dec 2005
    Beans
    Hidden!
    Distro
    Kubuntu

    Re: Bash script to recursively display duration of MP3 & MP4 files

    That final solution isn't putting out a TAB in between the filename and the duration

    Code:
    #!/bin/bash
    find ~/root/dir -name "*.mp3" -o -name "*.mp4" | while read file;
    do
     duration=$(ffprobe "$file" 2>&1 | awk '/Duration/ { print $2 }')
     echo -e $file"\t"$duration
    done | sort -n
    I have tried modifying it to use
    Code:
    printf
    but not sure how to use variables with "printf"

  5. #5
    Join Date
    Dec 2007
    Beans
    12,521

    Re: Bash script to recursively display duration of MP3 & MP4 files

    Quote Originally Posted by oygle View Post
    .... Also, I wanted to copy the output into a spreadsheet, so that the filename is first, then the duration. Is that simply a matter of adding a TAB character so that an import to the spreadsheet will be placed into columns.
    A tab isn't compulsory if you're using LibreOffice Calc.

    You can use any other character that won't occur in your output. You could try #.
    Attached Images Attached Images

  6. #6
    Join Date
    Dec 2005
    Beans
    Hidden!
    Distro
    Kubuntu

    Re: Bash script to recursively display duration of MP3 & MP4 files

    Quote Originally Posted by vasa1 View Post
    A tab isn't compulsory if you're using LibreOffice Calc.

    You can use any other character that won't occur in your output. You could try #.
    I use LibreOffice Calc and the "#" worked just fine, thanks very much.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •