简体   繁体   中英

compress video using ffmpeg then move to respective folder

I have a directory structure with raw .avi videos sitting inside those directories. I'm trying to create a script that will:

  1. Copy the directory structure to a NAS
  2. Compress the raw .avi videos to .mp4 format
  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)
  4. Delete the original raw .avi video

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:

  1. Create 'longdirectoryinhashformat1' on NAS
  2. Compress all .avi files into the same directory
  3. Move all compressed (.mp4) files into the directory with same name on NAS
  4. Delete the original directory and contents
  5. Create 'longdirectoryinhashformat2' on NAS
  6. ...rinse and repeat until nothing is left on the source computer.

Note: This will be run in a cron job (or Windows equivalent) nightly.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM