如果可能的话,我想知道如何才能知道在 Linux 文件管理器中选定的音频/视频文件的总时长。
我使用 Ubuntu 18.04 LTS,其默认文件管理器是 Files AKA鹦鹉螺。
答案1
这是 Nautilus 脚本
- 在 nautilus 中选择文件
- 右键单击它
- 在上下文菜单中,选择脚本 >脚本名称
安装:
安装媒体信息如果不存在则将sudo apt install mediainfo
脚本保存到文件中,~/.local/share/nautilus/scripts
使其可执行,chmod +x ~/.local/share/nautilus/scripts/scriptname
使用 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}"
答案2
很棒的脚本,我是编码新手,我刚刚替换了一些代码,非常感谢
#!/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