在Excel中用VBA实现提取视频的时长,音频分辩率,帧宽度,帧高度,视频比特率,路径地址,视频名称,视频比例等信息录到表格中,并以当前日期时间为名称生成新的表格文件。
时间: 2024-03-24 08:36:24 浏览: 209
以下是一个VBA代码示例,可以实现提取视频的时长、音频分辨率、帧宽度、帧高度、视频比特率等信息,并将这些信息记录在一个新的Excel表格中。同时,将该表格以当前日期时间为名称保存在指定的文件夹中。
```
Sub VideoInfo()
'定义变量
Dim fso As Object '文件系统对象
Dim folder As Object '文件夹对象
Dim file As Object '文件对象
Dim shell As Object 'Shell对象
Dim objFolder As Object 'Shell文件夹对象
Dim objFolderItem As Object 'Shell文件夹项对象
Dim objFile As Object 'Shell文件对象
Dim objFolderItems As Object 'Shell文件夹项集合对象
Dim objShellApp As Object 'Shell应用程序对象
Dim objShellFolder As Object 'Shell文件夹对象
Dim objShellFolderItem As Object 'Shell文件夹项对象
Dim path As String '视频文件夹路径
Dim wb As Workbook '新建的Excel表格
Dim ws As Worksheet '工作表
Dim row As Integer '行号
Dim videoDuration As String '视频时长
Dim audioResolution As String '音频分辨率
Dim frameWidth As String '帧宽度
Dim frameHeight As String '帧高度
Dim videoBitrate As String '视频比特率
Dim videoPath As String '视频路径
Dim videoName As String '视频名称
Dim videoRatio As String '视频比例
'设置视频文件夹路径
path = "C:\Users\Administrator\Videos\"
'获取文件系统对象
Set fso = CreateObject("Scripting.FileSystemObject")
'获取视频文件夹对象
Set folder = fso.GetFolder(path)
'新建Excel表格
Set wb = Workbooks.Add
'设置工作表
Set ws = wb.Sheets(1)
'设置标题栏
ws.Cells(1, 1).Value = "视频名称"
ws.Cells(1, 2).Value = "视频路径"
ws.Cells(1, 3).Value = "视频时长"
ws.Cells(1, 4).Value = "音频分辨率"
ws.Cells(1, 5).Value = "帧宽度"
ws.Cells(1, 6).Value = "帧高度"
ws.Cells(1, 7).Value = "视频比特率"
ws.Cells(1, 8).Value = "视频比例"
row = 2 '从第二行开始记录视频信息
'遍历视频文件夹中的所有视频文件
For Each file In folder.Files
'获取视频文件的路径
videoPath = file.Path
'获取视频文件的名称
videoName = file.Name
'使用Shell对象获取视频信息
Set shell = CreateObject("Shell.Application")
Set objFolder = shell.Namespace(fso.GetParentFolderName(videoPath))
Set objFolderItems = objFolder.Items
For Each objFolderItem In objFolderItems
If objFolderItem.Name = fso.GetFileName(videoPath) Then
Set objFile = objFolderItem
Exit For
End If
Next
Set objShellApp = CreateObject("Shell.Application")
Set objShellFolder = objShellApp.Namespace(fso.GetParentFolderName(videoPath))
Set objShellFolderItem = objShellFolder.ParseName(fso.GetFileName(videoPath))
'获取视频时长
videoDuration = objShellFolder.GetDetailsOf(objShellFolderItem, 27)
'获取音频分辨率
audioResolution = objShellFolder.GetDetailsOf(objShellFolderItem, 28)
'获取帧宽度
frameWidth = objShellFolder.GetDetailsOf(objShellFolderItem, 31)
'获取帧高度
frameHeight = objShellFolder.GetDetailsOf(objShellFolderItem, 32)
'获取视频比特率
videoBitrate = objShellFolder.GetDetailsOf(objShellFolderItem, 36)
'计算视频比例
If frameHeight <> "" And frameWidth <> "" Then
videoRatio = frameWidth & ":" & frameHeight
End If
'将视频信息记录到Excel表格中
ws.Cells(row, 1).Value = videoName
ws.Cells(row, 2).Value = videoPath
ws.Cells(row, 3).Value = videoDuration
ws.Cells(row, 4).Value = audioResolution
ws.Cells(row, 5).Value = frameWidth
ws.Cells(row, 6).Value = frameHeight
ws.Cells(row, 7).Value = videoBitrate
ws.Cells(row, 8).Value = videoRatio
row = row + 1 '下一行记录
Next
'保存Excel表格
wb.SaveAs "C:\Users\Administrator\Videos\VideoInfo_" & Format(Now(), "yyyymmdd_hhmmss") & ".xlsx"
'关闭Excel表格
wb.Close
'释放对象
Set fso = Nothing
Set folder = Nothing
Set file = Nothing
Set shell = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objFile = Nothing
Set objFolderItems = Nothing
Set objShellApp = Nothing
Set objShellFolder = Nothing
Set objShellFolderItem = Nothing
End Sub
```
在运行该代码之前,需要将视频文件夹路径修改为实际路径,并确保视频文件夹中至少包含一个视频文件。执行该代码后,会在指定的视频文件夹中生成一个新的Excel表格,其中记录了视频的各种信息,并以当前日期时间为名称保存。
阅读全文