shell 递归列出目录中所有文件的视频持续时间

iezvtpos  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(120)

我想列出一个文件夹中每个视频的所有文件名和持续时间。目前,我只能针对单个文件:
ffmpeg -i intro_vid001.mp4 2>&1| grep持续时间
有人能建议我如何在终端上打印出来,或者为文件夹中的每个视频文件打印到文本文件中吗?
我尝试过使用shell脚本,但我对shell脚本非常陌生。

if [ -z $1 ];then echo Give target directory; exit 0;fi

find "$1" -depth -name ‘*’ | while read file ; do
directory=$(dirname "$file")
oldfilename=$(basename "$file")

echo oldfilename
#ffmpeg -i $directory/$oldfilename” -ab 320k “$directory/$newfilename.mp3″ </dev/null
ffmpeg -i "$directory/$oldfilename" 2>&1 | grep Duration | echo
#rm “$directory/$oldfilename”

done
ssgvzors

ssgvzors1#

1.我忘了一美元在前面的一个'oldfilename'

  1. grep写在它的stdout上,你不能用echo管道不使用它的stdin。
    我建议使用以下脚本:
find "$1" -type f | while read videoPath ; do
    videoFile=$(basename "$videoPath")
    duration=$(ffmpeg -i "$videoPath" 2>&1 | grep Duration)

    echo "$videoFile: $duration"
done
kmpatx3s

kmpatx3s2#

如何创建一个文件夹的所有视频文件在一个目录中包含其持续时间(长度)在h:mm:ss?
在网上看了几个提示后,这些提示都不适合我,我终于能够创建这个脚本,它在Windows 11下使用Power Shell在我这边工作。也许你也能从中受益。
用途:

  • 确保已安装ffmpeg
  • 确保在.ps1文件中正确设置了ffprobe.exe(ffmpeg的一部分)的路径
  • 确保在.ps1文件中正确提及了工作目录
  • 然后以管理员身份运行powershell
  • 导航到目标目录
  • 执行.\f2\f25 FileListWithDuration.ps1

它应该创建一个文件VideoFileListWithDurations.txt作为所需数据的输出,您可以轻松地将其复制到Excel。
好好享受吧!
保存此脚本在目录中与您的视频-作为“文件夹FileListWithDuration.ps1”:

# Set the path to the directory you want to list files from
$directoryPath = "[enter your dir path here where video files are located]"

# Set the path to the output file
$outputFilePath = "$directoryPath\VideoFileListWithDurations.txt"

try {
    # Get all video files in the specified directory
    $videoFiles = Get-ChildItem -Path $directoryPath | Where-Object {
        !$_.PSIsContainer -and $_.Extension -match '\.(mp4|avi|mkv|wmv)$'
    }

    # Initialize an array to store file information strings
    $fileInfoStrings = @()

    # Add headers to the array
    $fileInfoStrings += "FileName`tFileSize (MB)`tFileType`tCreated`tLastModified`tDuration"


    # Loop through each video file and retrieve its information
    foreach ($file in $videoFiles) {
        $fileInfo = @{
            FileName = $file.Name
            FileSize = "{0:N3}" -f ($file.Length / 1MB) # Format in megabytes with 3 decimal places
            FileType = $file.Extension
            Created = $file.CreationTime
            LastModified = $file.LastWriteTime
            Duration = "N/A"
        }

        try {
            Write-Host "Getting duration for $($file.Name)"
            $ffprobeOutput = & [enter path to ffprobe.exe file here - without ""] -i $($file.FullName) -show_entries format=duration -v quiet -of csv="p=0"
            Write-Host "ffprobe output: $ffprobeOutput"
            $duration = [double]$ffprobeOutput.Trim()
            $timeSpan = [TimeSpan]::FromSeconds($duration)
            $fileInfo.Duration = $timeSpan.ToString("h\:mm\:ss")
        } catch {
            $fileInfo.Duration = "Error getting duration"
        }

        $fileInfoStrings += "$($fileInfo.FileName)`t$($fileInfo.Duration)`t$($fileInfo.FileSize)"
    }

    # Export the file information strings to the output file
    $fileInfoStrings | Out-File -FilePath $outputFilePath -Append

    Write-Host "File information exported to $outputFilePath"
} catch {
    Write-Host "An error occurred: $_"
    Read-Host "Press Enter to exit"
}

相关问题