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:
- Reads file paths from the clipboard.
- Creates a text file (
mylist.txt
) listing video files for FFmpeg. - Concatenates videos into a single file (
output.mkv
oroutput.mp4
) using FFmpeg. - Renames existing output files with a timestamp to avoid overwriting.
- 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:
- 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
).
- 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 . """"
- Fix: Install FFmpeg and add it to PATH, or specify the full path:
- 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%"""
- Fix: Install mpv and add to PATH, or specify the full path:
- 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
).
- Fix: Test with paths relative to the user’s video folder (e.g.,
- 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
).
- AutoHotkey:
- The script requires AutoHotkey to run.
- Fix: Install AutoHotkey from https://www.autohotkey.com/download/ahk-install.exe
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
- Install AutoHotkey, FFmpeg, and mpv.
- 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.)
- Run the script.
- Check the output video (
output.mkv
oroutput.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!