AutoHotkey 语言 批量提取视频播放时长的实践

AutoHotkey阿木 发布于 2025-06-12 10 次阅读


AutoHotkey Language: Extracting Video Play Time in Batch

Introduction

AutoHotkey is a powerful scripting language for automating tasks on Windows. It allows users to create scripts that can automate repetitive tasks, simulate keyboard and mouse inputs, and interact with various applications. In this article, we will explore how to use AutoHotkey to create a script that can batch extract video play times from a list of video files.

Why Extract Video Play Times?

Extracting video play times can be useful for various reasons, such as:

- Creating a playlist with accurate durations.
- Organizing video files based on their lengths.
- Analyzing video content for research purposes.

Prerequisites

Before we dive into the code, ensure you have the following prerequisites:

- AutoHotkey installed on your Windows machine.
- A list of video files you want to extract play times from.
- The video files should be in a compatible format for the script to work.

Step-by-Step Guide to Creating the Script

Step 1: Set Up Your AutoHotkey Environment

First, make sure you have AutoHotkey installed. You can download it from the official website: https://www.autohotkey.com/

Step 2: Create a New Script

Open Notepad or any text editor and save the file with a `.ahk` extension, for example, `ExtractVideoPlayTimes.ahk`.

Step 3: Write the Script

Below is a sample script that extracts video play times from a list of video files. This script uses the `ffmpeg` command-line tool to get the video duration. Make sure you have `ffmpeg` installed on your system.

ahk
NoEnv
SingleInstance, Force
SetWorkingDir, %A_ScriptDir%

; Define the list of video files
videoFiles := ["video1.mp4", "video2.mkv", "video3.avi"]

; Loop through each video file
Loop, % videoFiles.MaxIndex()
{
videoFile := videoFiles[A_Index]
; Extract the video duration using ffmpeg
duration := Run("ffmpeg -i """ . videoFile . """ 2>&1", "", "Hide")
; Wait for ffmpeg to finish
Sleep, 1000
; Parse the output for the duration
RegExMatch(duration, "Duration: (d{2}:d{2}:d{2}.d{2})", match)
; Store the duration in a new array
durations[A_Index] := match1
}

; Output the results
MsgBox, The play times are:
Loop, % durations.MaxIndex()
{
MsgBox, % "Video " . A_Index . ": " . durations[A_Index]
}

Step 4: Run the Script

Double-click the `ExtractVideoPlayTimes.ahk` file to run the script. A message box will display the play times for each video file in the list.

Explanation of the Script

Here's a breakdown of the script's functionality:

- `NoEnv` and `SingleInstance, Force` are directives that optimize the script's performance.
- `SetWorkingDir, %A_ScriptDir%` sets the script's working directory to the same directory as the script file.
- `videoFiles` is an array that contains the paths to the video files.
- The `Loop` statement iterates through each video file in the `videoFiles` array.
- `Run("ffmpeg -i """ . videoFile . """ 2>&1", "", "Hide")` executes the `ffmpeg` command to get the video duration and captures the output.
- `Sleep, 1000` waits for one second to ensure `ffmpeg` has finished processing.
- `RegExMatch(duration, "Duration: (d{2}:d{2}:d{2}.d{2})", match)` uses a regular expression to extract the video duration from the `ffmpeg` output.
- `durations[A_Index] := match1` stores the extracted duration in a new array called `durations`.
- The final `Loop` statement outputs the play times for each video file.

Conclusion

In this article, we've explored how to use AutoHotkey to create a script that can batch extract video play times from a list of video files. By utilizing the `ffmpeg` command-line tool, we were able to automate the process and save time. This script can be further customized to suit your specific needs, such as adding error handling, supporting additional video formats, or integrating with other applications. Happy scripting!