2

I would like to know how I can know the total duration of selected audio/video files in one of the Linux file managers, if it's possible.

I use Ubuntu 18.04 LTS, and its default file manager is Files AKA Nautilus.

cmak.fr
  • 8,411
  • 2
  • 29
  • 45
  • You asking about the Duration? – Razor Rassh Aug 02 '19 at 22:28
  • 1
    Yes about the Duration –  Aug 02 '19 at 22:28
  • 1
    Recommended you to view this thread: https://askubuntu.com/a/959538/978216 – Razor Rassh Aug 02 '19 at 22:34
  • This answer is not about the file manager –  Aug 02 '19 at 23:03
  • See https://superuser.com/questions/755690/display-audio-video-stream-title-and-duration-of-file-in-nautilus-folder – K7AAY Aug 02 '19 at 23:26
  • Thank you for your response K7AAY but is any way to see duration of multi file? If you see my question on more time you can see I told multi file. This mean select multi file and check duration of all of the selected files –  Aug 03 '19 at 01:43
  • I spent an hour on this and only got so far. Dolphin file manager works with MediaInfo, right click on a single media item and get the duration. it also has a column of duration which doesn't show anything for me. If mediainfo is not in right click context menu , install it. sudo apt install mediainfo mediainfo-gui. in dolphin right click a column bar and audio, duration. I am not sure if that goes for videos or not. mediainfo-gui run by itself takes a lllooooonnng time for 50 odd media files. – pierrely Aug 03 '19 at 06:29
  • https://wiki.archlinux.org/index.php/Baloo indexes and has something to do with it. – pierrely Aug 03 '19 at 06:30
  • You want to select some files (audio or video) and get total duration? this must be scripted as nautilus script. See https://help.ubuntu.com/community/NautilusScriptsHowto. get audiovideo file duration in milliseconds with `mediainfo --Output='General;%Duration%' /path/to/file.ext` - `sudo apt install mediainfo` if not installed – cmak.fr Aug 03 '19 at 07:03

2 Answers2

3

Here is a Nautilus Script
- Select files in nautilus
- Right-click on it
- In context menu, select Script > NameOfScript

enter image description here


Installation:
Install the mediainfo program if not present sudo apt install mediainfo
Save the script to a file in ~/.local/share/nautilus/scripts
Make it executable chmod +x ~/.local/share/nautilus/scripts/scriptname
Visit the scripts directory once with nautilus nautilus ~/.local/share/nautilus/scripts

#!/bin/bash

# Selected fileslist to Array
OLDIFS=$IFS
IFS=$'\n'
fileArray=($NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)
IFS=$OLDIFS

# Length of array: total num of selected files
NbFiles=${#fileArray[@]}

# The loop 
for (( i=0; i < ${NbFiles}; i++ ));
do
    # Get duration if file is media (audio or video)
    buff=$(file -N -i "${fileArray[$i]}" | grep -E 'audio|video')
    if [ ! -z  "$buff" ]
        then
            # mediainfo gives duration in milliseconds, easy to sum up
            MediaDuration=$(mediainfo --Output='General;%Duration%' "${fileArray[$i]}")
            TotalDuration=$((TotalDuration + MediaDuration))
            NbMedia=$((NbMedia + 1))
    fi
done

# Format Duration: milliseconds to H:M:S
Seconds=$((TotalDuration / 1000))
FormattedDuration=$(printf '%02dh:%02dm:%02ds\n' $(($Seconds/3600)) $(($Seconds%3600/60)) $(($Seconds%60)))

# Build report
ReportText="${NbFiles} File"
test $NbFiles -gt 1 && ReportText="${ReportText}s"
ReportText="${ReportText} selected\n"
test $NbMedia -gt 0 && ReportText="${ReportText}${NbMedia} media file" || ReportText="${ReportText}No media file"
test $NbMedia -gt 1 && ReportText="${ReportText}s"
test $NbMedia -gt 0 && ReportText="${ReportText}\nTotal duration: ${FormattedDuration}"

zenity  --info --no-wrap --text="${ReportText}"
cmak.fr
  • 8,411
  • 2
  • 29
  • 45
-1

Nice script, Im new with coding and ijust replaced some of the code Thank you a lot

#!/bin/bash

# Selected fileslist to Array
IFS=$'\n'
fileArray=($(find $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS -type f | grep -iE "\.webm$|\.flv$|\.vob$|\.ogg$|\.ogv$|\.drc$|\.gifv$|\.mng$|\.avi$|\.mov$|\.qt$|\.wmv$|\.yuv$|\.rm$|\.rmvb$|/.asf$|\.amv$|\.mp4$|\.m4v$|\.mp4$|\.m?v$|\.svi$|\.3gp$|\.flv$|\.f4v$"))
audioArray=($(find $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS -type f | grep -iE "\.pcm$|\.raw$|\.wav$|\.aif$|\.aiff$|\.aifc$|\.caf$|\.au$|\.snd$|\.mp3$|\.m4a$|\.m4b$|\.m4p$|\.m4v$|\.m4r$|\.aac$|\.wma$|\.ogg$|\.oga$|\.flac$"))
# Length of array: total num of selected files
Nbfiles=-1
audioFiles=-1
NbFiles=${#fileArray[@]}
audioFiles=${#audioArray[@]}

# The loop audio and video

for (( i=0; i < ${NbFiles}; i++ ));

do
    # mediainfo gives duration in milliseconds, easy to sum up
    MediaDuration=$(mediainfo --Output='General;%Duration%' "${fileArray[$i]}")
    TotalDuration=$((TotalDuration + MediaDuration))
    NbMedia=$((NbMedia + 1))
done

for (( i=0; i < ${audioFiles}; i++ ));
do
    # mediainfo gives duration in milliAudioSeconds, easy to sum up
    AudioMediaDuration=$(mediainfo --Output='General;%Duration%' "${audioArray[$i]}")
    AudioTotalDuration=$((AudioTotalDuration + AudioMediaDuration))
    audioMedia=$((audioMedia + 1))
   
done

# Format Duration: milliseconds to H:M:S
Seconds=$((TotalDuration / 1000))
FormattedDuration=$(printf '%02dh:%02dm:%02ds\n' $(($Seconds/3600)) $(($Seconds%3600/60)) $(($Seconds%60)))
# Format Duration: milliAudioSeconds to H:M:S
AudioSeconds=$((AudioTotalDuration / 1000))
AudioFormattedDuration=$(printf '%02dh:%02dm:%02ds\n' $(($AudioSeconds/3600)) $(($AudioSeconds%3600/60)) $(($AudioSeconds%60)))

# Video report
if [ $NbFiles -gt 0 ] 
then
    if [ $NbMedia -gt 0 ]
        then
        ReportText="\n Video \n \n${ReportText}${NbMedia} Video file" || ReportText="${ReportText}No Video file"
    fi
    if [ $NbMedia -gt 1 ] 
        then
        ReportText="${ReportText}s"
    fi
    if [ $NbMedia -gt 0 ] 
        then
        ReportText="${ReportText}\nTotal Video duration: ${FormattedDuration}"
    fi
fi
# Audio report
if [ $audioFiles -gt 0 ]
then
    if [ $audioMedia -gt 0 ]
        then
        AudioReportText="\n\nAudio \n \n${AudioReportText}${audioMedia} Audio file" 
        else
        AudioReportText="${AudioReportText}No Audio file"
    fi
    if [ $audioMedia -gt 1 ] 
        then AudioReportText="${AudioReportText}s" 
    fi
    if [ $audioMedia -gt 0 ]
        then
        AudioReportText="${AudioReportText}\nTotal Audio duration: ${AudioFormattedDuration}"
    fi
fi
#Total time of audio and video
if [ $NbFiles -gt 0 ] && [ $audioFiles -gt 0 ]
    then
    totalfiles=$((NbMedia +  audioMedia))
    TotalSeconds=$((Seconds +  AudioSeconds))
    TotalFormattedDuration=$(printf '%02dh:%02dm:%02ds\n' $(($TotalSeconds/3600)) $(($TotalSeconds%3600/60)) $(($TotalSeconds%60)))
    TotalReportText="\n \n Total Files: ${totalfiles}\n Total Time:${TotalFormattedDuration}"   
fi
#if no file is found print text or else print everything
if [ $NbFiles -eq 0 ] && [ $audioFiles -eq 0 ]
    then
    ReportText="No Media Found"
    zenity  --info --no-wrap --text="${ReportText} ${AudioReportText}"
    else
    # Format Duration: milliseconds to H:M:S
    zenity  --info --no-wrap --text="${ReportText} ${AudioReportText} ${TotalReportText}"
fi
vag
  • 1
  • 1
  • 1
    Welcome to Ask Ubuntu! Please don't add "thanks" as answers. Invest some time in the site and you will gain sufficient [privileges](//askubuntu.com/privileges) to upvote answers you like, which is the Ask Ubuntu way of saying thank you. – Martin Thornton Sep 24 '20 at 22:43