Skip to content

Instantly share code, notes, and snippets.

@v-mas
Created January 29, 2018 00:31
Show Gist options
  • Save v-mas/561c4f42785c5b284f1e5586bb8df806 to your computer and use it in GitHub Desktop.
Save v-mas/561c4f42785c5b284f1e5586bb8df806 to your computer and use it in GitHub Desktop.
using VLC to convert mpg/mov files into mp4 and copy all the rest
####
# if mp4a audio is not working, open VLC and go to
# Tools > Preferences > Input/Codecs > Video codecs > FFmpeg
# and change
# Strict standard compliance
# to
# -2
####
Param(
[string]$if,
[string]$of = $if + "_converted",
[switch]$help = $false
)
$VLC_EXE = $env:ProgramFiles + "\VideoLAN\VLC\vlc.exe"
$BITRATE = 64
$OVERWRITE_CREATION_TIME = $true
function Print-Help() {
"
This script will convert all .mpg and .mov files in specified directory to .mp4 and copy all other files
Parameters:
-if Input folder [required] folder in which scipt will search for fiels and will make copy or convert
-of Output folder [optional, default `"{if}_convert`"] taget folder to which script will save copied/converted files
-help Print out this help message
if mp4a audio is not working, open VLC and go to
Tools > Preferences > Input/Codecs > Video codecs > FFmpeg
and change
Strict standard compliance
to
-2
"
}
function Video-Convert {
Param(
[System.IO.FileInfo]$file,
[System.IO.DirectoryInfo]$saveDir,
[switch]$deinterlace = $false
)
"converting " + $file.FullName
$extraTranscodeArgs = ""
if ($deinterlace) {
$extraTranscodeArgs += ",deinterlace=`"1`""
}
$outputFileName = Join-Path -Path $saveDir -ChildPath ($file.BaseName + ".mp4")
$args = "-I dummy -vvv `"$($file.FullName)`" --sout-ffmpeg-strict=-2 --sout=#transcode{vcodec=`"h264`",acodec=`"aac`",ab=`"$BITRATE`"$($extraTranscodeArgs)}:standard{access=`"file`",mux=`"mp4`",dst=`"$outputFileName`"} vlc://quit"
Start-Process $VLC_EXE $args -Wait
$outFile = Get-Item $outputFileName
$outFile.LastWriteTime = $file.LastWriteTime
if ($OVERWRITE_CREATION_TIME) {
$outFile.CreationTime = $file.LastWriteTime
}
}
function Handle-Dir([System.IO.DirectoryInfo]$inDir, [System.IO.DirectoryInfo]$outDir) {
foreach($file in $inDir.GetFileSystemInfos()) {
if ($file.getType() -eq [System.IO.DirectoryInfo]) {
Handle-Dir -inDir $file -outDir $outDir.CreateSubdirectory($file.BaseName)
} else {
Handle-File -file $file -saveDir $outDir
}
}
}
function Handle-File([System.IO.FileInfo]$file, [System.IO.DirectoryInfo]$saveDir) {
switch($file.Extension) {
".jpg" {
Handle-JPG -file $file -saveDir $saveDir
}
".mov" {
Handle-MOV -file $file -saveDir $saveDir
}
".mpg" {
Handle-MPG -file $file -saveDir $saveDir
}
default {
Handle-UnknownFile -file $file -saveDir $saveDir
}
}
}
function Handle-UnknownFile([System.IO.FileInfo]$file, [System.IO.DirectoryInfo]$saveDir) {
"copying " + $file.FullName
Copy-Item -Path $file.FullName -Destination $saveDir
if ($OVERWRITE_CREATION_TIME) {
(Get-Item (Join-Path -Path $saveDir -ChildPath ($file.Name))).CreationTime = $file.LastWriteTime
}
}
function Handle-JPG([System.IO.FileInfo]$file, [System.IO.DirectoryInfo]$saveDir) {
Handle-UnknownFile -file $file -saveDir $saveDir
}
function Handle-MOV([System.IO.FileInfo]$file, [System.IO.DirectoryInfo]$saveDir) {
Video-Convert -file $file -saveDir $saveDir
}
function Handle-MPG([System.IO.FileInfo]$file, [System.IO.DirectoryInfo]$saveDir) {
Video-Convert -file $file -saveDir $saveDir -deinterlace
}
if (!$if -or $help) {
Print-Help
exit
}
if (!(Test-Path -Path $if) -or !((Get-Item $if).Attributes -match "directory")) {
"input is not correct"
exit
}
if (Test-Path -Path $of) {
"WARNING " + $output + " already exists, remove it first"
exit
}
if (!(Test-Path -Path $VLC_EXE)) {
"please check VLC path"
exit
}
$input = Get-Item $if
$output = New-Item -Path $of -ItemType "directory"
"we have the dir: " + $input.FullName
"saving converted to: " + $output.FullName
Handle-Dir -inDir $input -outDir $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment