Skip to content

Instantly share code, notes, and snippets.

@mjaroszek
Last active August 29, 2023 00:17
Show Gist options
  • Save mjaroszek/43d603bb90cf8823366c7793e1634c24 to your computer and use it in GitHub Desktop.
Save mjaroszek/43d603bb90cf8823366c7793e1634c24 to your computer and use it in GitHub Desktop.
mp4_copy.ps1
if ($args.Length -ne 1) {
Write-Error "Only one argument required - working dir"
exit 1
}
$working_dir = $args[0]
$working_dir = [IO.Path]::GetFullPath(${working_dir})
if (-not (Test-Path -Path ${working_dir})) {
Write-Error "Directory does not exist: ${working_dir}"
exit 2
}
Write-Host "Working directory: ${working_dir}"
Write-Host
$file_list = @()
(Get-ChildItem (Join-Path ${working_dir} /*) -Include *.mp4) | ForEach-Object {
$file_list += $_
}
Write-Host "Found files:" ${file_list}.Length
$file_list_length = ${file_list}.Length
$file_list | ForEach-Object {
Write-Host $_.Name
}
Write-Host
$confirm = Read-Host -Prompt "Are you sure you want to proceed? [y/N]"
if ($confirm -ne 'y') {
exit 0
}
New-Item -ItemType Directory -Force -Path (Join-Path ${working_dir} "/processed") | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path ${working_dir} "/output") | Out-Null
$i = 0
$file_list | ForEach-Object {
$i = $i + 1
Write-Host
Write-Host "Processing file ${i}/${file_list_length}:" ${_}.Name
ffmpeg -i ${_} -c copy -map 0:a -map 0:v (Join-Path ${working_dir} "/output" ${_}.Name)
if (${LASTEXITCODE} -ne 0) {
Write-Error "Failed to process file (EXIT: ${LASTEXITCODE}): ${_}"
exit 3
}
Move-Item -LiteralPath ${_} (Join-Path ${working_dir} "/processed/")
}
PS [REDACTED]> dir ./Test/
Directory: [REDACTED]/Test
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 23:42 0 1.txt
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 23:42 0 2.txt
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 23:42 0 3.txt
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 23:42 0 4 0.txt
-rw-r--r-- [REDACTED] [REDACTED] 21/08/2023 13:04 66871970 file1.mp4
-rw-r--r-- [REDACTED] [REDACTED] 24/08/2023 08:16 34896221 file2.mp4
-rw-r--r-- [REDACTED] [REDACTED] 21/03/2021 14:30 12217885 file3.mp4
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 21:08 133175196 file4.mp4
PS [REDACTED]> ./mp4_copy.ps1 ./Test/
Working directory: [REDACTED]/Test/
Found files: 4
file1.mp4
file2.mp4
file3.mp4
file4.mp4
Are you sure you want to proceed? [y/N]: y
Processing file 1/4: file1.mp4
[ffmpeg output]
Processing file 2/4: file2.mp4
[ffmpeg output]
Processing file 3/4: file3.mp4
[ffmpeg output]
Processing file 4/4: file4.mp4
[ffmpeg output]
PS [REDACTED]> dir ./Test/
Directory: [REDACTED]/Test
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
drwxr-xr-x [REDACTED] [REDACTED] 29/08/2023 01:54 192 output
drwxr-xr-x [REDACTED] [REDACTED] 29/08/2023 01:54 192 processed
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 23:42 0 1.txt
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 23:42 0 2.txt
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 23:42 0 3.txt
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 23:42 0 4 0.txt
PS [REDACTED]> dir ./Test/output/
Directory: [REDACTED]/Test/output
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
-rw-r--r-- [REDACTED] [REDACTED] 29/08/2023 01:54 67362299 file1.mp4
-rw-r--r-- [REDACTED] [REDACTED] 29/08/2023 01:54 35282967 file2.mp4
-rw-r--r-- [REDACTED] [REDACTED] 29/08/2023 01:54 12477979 file3.mp4
-rw-r--r-- [REDACTED] [REDACTED] 29/08/2023 01:54 133591845 file4.mp4
PS [REDACTED]> dir ./Test/processed/
Directory: [REDACTED]/Test/processed
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
-rw-r--r-- [REDACTED] [REDACTED] 21/08/2023 13:04 66871970 file1.mp4
-rw-r--r-- [REDACTED] [REDACTED] 24/08/2023 08:16 34896221 file2.mp4
-rw-r--r-- [REDACTED] [REDACTED] 21/03/2021 14:30 12217885 file3.mp4
-rw-r--r-- [REDACTED] [REDACTED] 28/08/2023 21:08 133175196 file4.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment