AutoHotkey script to concatenate AgentDVR videos

This AutoHotkey script concatenates video files using FFmpeg and plays the output with mpv. Below is a breakdown including what needs editing for use on another computer.


What the Script Does

The script:

  1. Reads file paths from the clipboard.
  2. Creates a text file (mylist.txt) listing video files for FFmpeg.
  3. Concatenates videos into a single file (output.mkv or output.mp4) using FFmpeg.
  4. Renames existing output files with a timestamp to avoid overwriting.
  5. Plays the resulting video with mpv.

Script Breakdown

  • Input Check: Ensures the clipboard contains file paths and the first path includes D:\windows (a specific folder check).
  • Folder Processing:
  • Strips quotes from the base path and sets it as the working directory.
  • Loops through clipboard lines, formats paths for mylist.txt (e.g., file 'video1.mp4').
  • Sorts the file list and writes it to mylist.txt.
  • Detects if .mkv files are present to set output format (.mkv or .mp4).
  • Runs FFmpeg with -f concat to merge files without re-encoding (-c copy).
  • Plays the output with mpv.
  • File Management: If the output file exists, it’s renamed with a timestamp (e.g., output_2025-05-11-14-30.mp4).

What Needs Editing

To make the script work on another computer, modify these elements:

  1. Clipboard Path Check:
  • The script checks for D:\windows in the first clipboard line. Replace with a relevant path or remove the check:
    autohotkey if (!InStr(firstLine, "D:\windows")) { ExitApp }
    Fix: Comment out or replace "D:\windows" with the user’s base path (e.g., C:\Videos).
  1. FFmpeg Path:
  • The script assumes ffmpeg is in the system’s PATH. Ensure FFmpeg is installed and accessible.
    • Fix: Install FFmpeg and add it to PATH, or specify the full path:
      autohotkey ffmpegCmd := """C:\path\to\ffmpeg.exe"" -f concat -safe 0 -i """ . listFile . """ -c copy """ . FileName . """"
  1. mpv Path:
  • The script uses mpv.exe to play the output. Ensure mpv is installed and in PATH.
    • Fix: Install mpv and add to PATH, or specify the full path:
      autohotkey Run, """C:\path\to\mpv.exe"" ""%FileName%"""
  1. Base Path:
  • The script derives the base path from the clipboard. Ensure clipboard paths match the user’s folder structure.
    • Fix: Test with paths relative to the user’s video folder (e.g., C:\Videos\video1.mp4).
  1. File Extensions:
  • The script assumes .mkv or .mp4. If other formats are used, modify the extension logic:
    autohotkey isMKV := InStr(a, ".mkv") FileName := isMKV ? basePath . "output.mkv" : basePath . "output.mp4"
    Fix: Add checks for other extensions (e.g., .avi, .mov).
  1. AutoHotkey:

Example Customization

For a user with videos in C:\Videos, FFmpeg at C:\FFmpeg\bin\ffmpeg.exe, and mpv at C:\mpv\mpv.exe:

if (!InStr(firstLine, "C:\Videos")) ; Update path
{
    ExitApp
}
ffmpegCmd := """C:\FFmpeg\bin\ffmpeg.exe"" -f concat -safe 0 -i """ . listFile . """ -c copy """ . FileName . """"
Run, """C:\mpv\mpv.exe"" ""%FileName%"""

Usage

  1. Install AutoHotkey, FFmpeg, and mpv.
  2. Copy paths of video files to the clipboard. (Select the videos and hold down Shift + Right Click and select Copy as path in the Windows Explorer context menu.)
  3. Run the script.
  4. Check the output video (output.mkv or output.mp4) in the base folder.

Notes

  • Ensure all video files have the same codec and resolution for seamless concatenation.
  • Test the script with a small set of files first.
  • Backup important files before running to avoid accidental overwrites.

ProcessFolder(basePath) {    
    ; Remove any leading/trailing quotes from basePath
    basePath := StrReplace(basePath, """", "")    
    SetWorkingDir %basePath%   
    a := ""
    insert := "file '"
    Loop, parse, Clipboard, `n, `r
    {
        if (A_LoopField != "")
        {
            filePath := StrReplace(A_LoopField, basePath, "")
            filePath := StrReplace(filePath, """", "")
            a .= insert . filePath . "'`r`n"
        }
    }   
    
    Clipboard := a
    Sort, a
    
    isMKV := InStr(a, ".mkv")  
    listFile := basePath . "mylist.txt"   
    FileDelete, %listFile%
    MyFileObj := FileOpen(listFile, "w")
    bytesWritten := MyFileObj.Write(a)
    MyFileObj.Close()
    Sleep, 500    
    FileName := isMKV ? basePath . "output.mkv" : basePath . "output.mp4"
    
    IfExist, %FileName%
    {
        SplitPath, FileName,, Dir, Ext, NameNoExt
        FormatTime, DateTimeNow,, yyyy-MM-dd-HH-mm
        NewFileName := Dir "\" NameNoExt "_" DateTimeNow "." Ext
        FileMove, %FileName%, %NewFileName%
    }    
    ffmpegCmd := "ffmpeg -f concat -safe 0 -i """ . listFile . """ -c copy """ . FileName . """"
	RunWait, cmd.exe /c %ffmpegCmd% 2>&1, , Hide
    Run, "mpv.exe" "%FileName%"   
    return
}

; Check if clipboard is empty
if (Clipboard = "")
{
    ExitApp
}

Loop, parse, Clipboard, `n, `r
{
    firstLine := A_LoopField
    break
}

if (!InStr(firstLine, "D:\windows"))
{
    ExitApp
}

SplitPath, firstLine,, basePath
basePath := RTrim(basePath, "\") . "\"

if (basePath)
    ProcessFolder(basePath)
else
    MsgBox, Could not determine folder path from clipboard!

Leave a Comment

Auto