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.
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.
Here is a Nautilus Script
- Select files in nautilus
- Right-click on it
- In context menu, select Script > NameOfScript
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}"
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