Reputation: 1
I have a directory structure with raw .avi videos sitting inside those directories. I'm trying to create a script that will:
Below is the ffmpeg command I'm using to compress the video.
ffmpeg -i <origVid>.avi -c:v h264 -crf 17 <convertedVid>.mp4
I found a couple of PowerShell snippets. The below code will copy the directory structure:
$sourceDir = "D:\Videos"
$targetDir = "H:\Converted"
Copy-Item $sourceDir $targetDir -Filter {PSIsContainer} -Recurse -Force
And the following snippet will compress videos inside individual folders
$origVids = Get-ChildItem .\*.avi -Recurse
foreach ($origVid in $origVids) {
$convertedVid = [io.path]::ChangeExtension($origVid, '.mp4')
.\ffmpeg.exe -i $origVid -c:v h264 -crf 17 $convertedVid
}
I could just move everything over to the NAS, and perform the conversion there and blow away the files in the directory on the source computer; however, that is a bit inefficient and would consume tons of bandwidth. I'd like to compress the video and then move the smaller converted file over to the NAS in their corresponding directory. Unfortunately, for the life of me, I can't figure out how to move the converted videos to their corresponding directories on the NAS. Any assistance would be greatly appreciated!
I should add, I'd like for this script to run recursively.
Example file directory structure:
longdirectorynameinhashformat1
video1a.avi
video2a.avi
video3a.avi
longdirectorynameinhashformat2
video1b.avi
video2b.avi
video3b.avi
My logic is as follows using the above folder structure, but not sure if it's possible writing in powershell or MS-DOS batch:
Note: This will be run in a cron job (or Windows equivalent) nightly.
Upvotes: 0
Views: 1256
Reputation: 1
Instead of using PowerShell, I'm using the following batch script, where "D:\" is the mapped NAS share I'll be storing the compressed videos. Although, I would like to know the PowerShell equivalent.
@echo off
Rem Replicate directory structure on NAS (where g: is the drive letter for the NAS share)
for /d %%d in (*.*) do mkdir g:\%%d
Rem FFmpeg processing for all files that end in *.avi. You can change *.avi to whatever extension you need.
call :treeProcess
goto :eof
:treeProcess
for %%f in (*.avi) do ffmpeg -i "%%f" -c:v h264 -crf 17 "g:\%%d\%%~nf".mp4
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Upvotes: 0
Reputation: 16096
Of course the ffmpeg is not in the control of PowerShell. As to you bullets ---
1.Copy the directory structure to a NAS Just use Copy-Item or robocopy
2.Compress the raw .avi videos to .mp4 format
Compress however you like
3.Move the converted .mp4 video to the corresponding directory on the NAS (the converted file needs to reside in a directory with the same name as the source directory)
Use Move-Item or robocopy
4.Delete the original raw .avi video
Use Remove-Item
Upvotes: 1