查找所有的视频文件#
以.mp4
为例,使用命令查找所有视频文件,其中 -xtype f
选项为可选,MacOS 环境下可能需要使用! -type l
替代-xtype f
1
|
find . -type f -name "*.mp4" -xtype f
|
-xtype f 选项的作用
表示只匹配实际上是文件的路径,而不匹配符号链接(symbolic link)。
在Unix/Linux系统中,文件类型有很多种,包括普通文件(regular file)、目录(directory)、符号链接(symbolic link)、管道(FIFO)、字符设备(character special file)和块设备(block special file)等等。`-xtype f`选项可以让`find`命令只匹配实际上是普通文件的路径,排除掉符号链接等其他文件类型的路径。
获取视频文件的时长信息#
使用xargs
命令将查找到的所有视频文件传递给ffprobe
命令
1
|
find . -type f -name "*.mp4" -xtype f | xargs -I{} ffprobe -i {} -show_entries format=duration -v quiet -of csv="p=0"
|
对视频总时长求和#
使用awk
命令将获取到的所有视频文件的时长信息相加,得到总时长,例如:
1
|
find . -type f -name "*.mp4" -xtype f | xargs -I{} ffprobe -i {} -show_entries format=duration -v quiet -of csv="p=0" | awk '{s+=$1} END {print "Total duration: "s" seconds"}'
|
如果需要将输出的时长以小时为单位,那么简单进行计算:
1
|
find . -type f -name "*.mp4" -xtype f | xargs -I{} ffprobe -i {} -show_entries format=duration -v quiet -of csv="p=0" | awk '{s+=$1} END {printf "Total duration: %.2f hours\n", s/3600}'
|