Skip to content

Commit

Permalink
v4.2.0 (#73)
Browse files Browse the repository at this point in the history
- Improved metadata parsing
- Bug fixes
   - Closes #67
   - Closes #68
   - Closes #70
  • Loading branch information
BrianDMG authored Aug 4, 2020
1 parent ae7d402 commit 9605ca2
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 29 deletions.
17 changes: 5 additions & 12 deletions conv2mp4-ps.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<#======================================================================================================================
conv2mp4-ps v4.1.2 - https://github.com/BrianDMG/conv2mp4-ps
conv2mp4-ps v4.2 - https://github.com/BrianDMG/conv2mp4-ps
This Powershell script will recursively search through a user-defined file path and convert all videos of user-specified
include_file_types to MP4 with H264 video and AAC audio using ffmpeg. If a conversion failure is detected, the script re-encodes
Expand Down Expand Up @@ -47,16 +47,16 @@ ForEach ($file in $fileList) {
$fileSubDirs = ($file.DirectoryName).Substring($cfg.media_path.Length, ($file.DirectoryName).Length - $cfg.media_path.Length)

If ($cfg.use_out_path) {
$targetPath = $targetFile = $cfg.out_path + $fileSubDirs + "\"
$targetPath = $cfg.out_path + $fileSubDirs + "\"

If (-Not (Test-Path $targetPath)) {
mkdir $targetPath -Force
}

$targetFile = $targetPath + $file.BaseName + "_NEW" + ".mp4"
$targetFile = $targetPath + $file.BaseName + ".mp4" + ".conv2mp4"
}
Else {
$targetFile = $file.DirectoryName + "\" + $file.BaseName + "_NEW" + ".mp4"
$targetFile = $file.DirectoryName + "\" + $file.BaseName + ".mp4" + ".conv2mp4"
}

$progress = ($(@($fileList).indexOf($file)+1) / $fileList.Count) * 100
Expand All @@ -71,13 +71,6 @@ ForEach ($file in $fileList) {
#Set targetFile final name
If ($cfg.use_out_path) {
$targetFileRenamed = $targetPath + $file.BaseName + ".mp4"

#If using out_path, create target path if it doesn't exist
If ($cfg.use_out_path) {
If (-Not (Test-Path $targetPath)) {
mkdir $$targetPath -Force
}
}
}
Else {
$targetFileRenamed = $file.DirectoryName + "\" + $file.BaseName + ".mp4"
Expand Down Expand Up @@ -201,7 +194,7 @@ ForEach ($file in $fileList) {
}

#If $sourceFile was an mp4, rename $targetFile to remove "-NEW"
$targetFileRenamed = "$targetFile" -replace "_NEW",""
$targetFileRenamed = "$targetFile" -replace ".conv2mp4",""
Move-Item $targetFile $targetFileRenamed

#If using out_path, delete empty source directories
Expand Down
2 changes: 1 addition & 1 deletion files/cfg/config.template
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ plex_token=plextoken

#Garbage collection
collect_garbage = false
garbage_include_file_types=*.nfo, *_NEW_
garbage_include_file_types=*.nfo, *.conv2mp4
6 changes: 3 additions & 3 deletions files/func/CloneStereoStream.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Function CloneStereoStream {
$ffmpegArgs += "-acodec "
$ffmpegArgs += "copy "
$ffmpegArgs += "$($prop.tmp_dir)\$($prop.tmp_51out)"
$ffmpegCMD = cmd.exe /c "$ffmpeg $ffmpegArgs"
$ffmpegCMD = cmd.exe /c "`"$ffmpeg`" $ffmpegArgs"

#ffmpeg convert audio track
$ffmpegArgs = "-i "
$ffmpegArgs += "$($prop.tmp_dir)\$($prop.tmp_51out) "
$ffmpegArgs += "-ac "
$ffmpegArgs += "2 "
$ffmpegArgs += "$($prop.tmp_dir)\$($prop.tmp_2in)"
$ffmpegCMD = cmd.exe /c "$ffmpeg $ffmpegArgs"
$ffmpegCMD = cmd.exe /c "`"$ffmpeg`" $ffmpegArgs"

#Rename original source file, necessary to avoid corrupting by using same file as input and output
Move-Item $targetFile $tempFileName -Force
Expand All @@ -45,7 +45,7 @@ Function CloneStereoStream {
$ffmpegArgs += "-metadata:s:a "
$ffmpegArgs += "handler=Stereo "
$ffmpegArgs += "$targetFile"
$ffmpegCMD = cmd.exe /c "$ffmpeg $ffmpegArgs"
$ffmpegCMD = cmd.exe /c "`"$ffmpeg`" $ffmpegArgs"

Remove-Item $tempFileName -Force
Remove-Item $prop.tmp_dir -Force -Recurse
Expand Down
69 changes: 60 additions & 9 deletions files/func/ConvertFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Function ConvertFile {
[Switch]$KeepSubs
)

$ffmpeg = Join-Path $cfg.fmmpeg_bin_dir "ffmpeg.exe"
$ffmpeg = Join-Path "$($cfg.fmmpeg_bin_dir)" "ffmpeg.exe"
$ffprobe = Join-Path "$($cfg.fmmpeg_bin_dir)" "ffprobe.exe"
$handbrake = Join-Path $cfg.handbrakecli_bin_dir "HandBrakeCLI.exe"

If ($ConvertType -eq "Handbrake") {
Expand Down Expand Up @@ -72,13 +73,53 @@ Function ConvertFile {
$ffArgs += "+faststart"

If ($cfg.use_set_metadata_title){
$remove= $title | Select-String -Pattern '^(.*?)(19|20)[0-9]{2}(.*$)' | ForEach-Object { "$($_.matches.groups[3])" }
$title = $title -replace "$remove",''
$title = $title -replace '\W',' '
$title = $title -replace '(\d{4})$', '($1)';

#Check if it's a TV episode
If ($title -match 's\d+'){
$regex = '^(.*?)(S\d+)(E\d+-?\s?E?\d*?)(\D+)(.*$)'
$unparsedTitle = $title
$remove = $title | Select-String -Pattern $regex | ForEach-Object { "$($_.matches.groups[5])" }
$title = $title -replace [Regex]::Escape("$remove"),''
$title = $title -replace '\W',' '
$title = $($title.trim() -replace "\s+"," ")
$showTitle = $title | Select-String -Pattern $regex | ForEach-Object { "$($_.matches.groups[1])" }
$seasonNumber = $title | Select-String -Pattern $regex | ForEach-Object { "$($_.matches.groups[2])" }
$seasonNumber = $seasonNumber -replace 's',''
$episodeNumber = $title | Select-String -Pattern $regex | ForEach-Object { "$($_.matches.groups[3])" }
$episodeNumber = $episodeNumber -replace 'e',''
$episodeNumber = $episodeNumber.trim() -replace '\W','-'
$episodeTitle = $title | Select-String -Pattern $regex | ForEach-Object { "$($_.matches.groups[4])" }

$ffArgs += "-metadata " #Flag to specify key/value pairs for encoding metadata
$ffArgs += "show=`"$($showTitle.trim())`" " #Use $showTitle variable as metadata 'show'
$ffArgs += "-metadata " #Flag to specify key/value pairs for encoding metadata
$ffArgs += "season_number=`"$('{0:d2}' -f [int]$seasonNumber)`" " #Use $seasonNumber variable as metadata 'season_number'
$ffArgs += "-metadata " #Flag to specify key/value pairs for encoding metadata
$ffArgs += "episode_id=`"$episodeNumber`" " #Use $episodeNumber variable as metadata 'episode_id'
$ffArgs += "-metadata " #Flag to specify key/value pairs for encoding metadata
$ffArgs += "title=`"$($episodeTitle.trim())`" " #Use $episodeTitleitle variable as metadata 'title'
$ffArgs += "-metadata " #Flag to specify key/value pairs for encoding metadata
$ffArgs += "description=`"$unparsedTitle`" " #Use $episodeTitleitle variable as metadata 'title'
}
#Otherwise it's assumed to be a movie
Else {
$regex = '^(.+)((19|20)\d{2})(.*$)'
$remove = $title | Select-String -Pattern $regex | ForEach-Object { "$($_.matches.groups[4])" }
$title = $title -replace [Regex]::Escape("$remove"),''
$title = $title -replace '\W',' '
$title = $($title.trim() -replace "\s+"," ")
$year = $($title.split()[-1])
$title = $title.SubString(0, $title.LastIndexOf(' '))

$ffArgs += "-metadata " #Flag to specify key/value pairs for encoding metadata
$ffArgs += "title=`"$title`" " #Use $title variable as metadata 'title'
$ffArgs += "-metadata " #Flag to specify key/value pairs for encoding metadata
$ffArgs += "date=`"$year`" " #Use $year variable as metadata 'date'
}

$encodeInformation = "Encoded by conv2mp4-$($prop.platform) v$($prop.version) ($($prop.github_url)) on $($time.Invoke())"
$ffArgs += "-metadata " #Flag to specify key/value pairs for encoding metadata
$ffArgs += "title=`"$title`" " #Use $title variable as metadata 'Title'
$ffArgs += "comment=`"$encodeInformation`" " #Use $encodingTool variable as metadata 'encoding_tool'
}

$ffArgs += "-map " #Flag to use channel mapping
Expand Down Expand Up @@ -109,15 +150,25 @@ Function ConvertFile {
}

If ($KeepSubs) {
$ffArgs += "-c:s " #Subtitle codec flag
$ffArgs += "mov_text " #Name of subtitle channel after export
$info = & $ffprobe -i $sourceFile 2>&1
#Detect if bitmap subs exist, and do not keep them if they do.
#Resolves error that causes ffmpeg to fail and launch failover
If (!$($info -Match '(pgssub|dvd_subtitle)')) {
$ffArgs += "-c:s " #Subtitle codec flag
$ffArgs += "mov_text " #Name of subtitle channel after export
}
Else {
Log "$($time.Invoke()) Detected bitmap subtitles, not keeping subtitles."
$ffArgs += "-sn " #Option to remove any existing subtitles
}
}
Else {
$ffArgs += "-sn " #Option to remove any existing subtitles
}
$ffArgs+= "-f mp4 "
$ffArgs += "`"$targetFile`"" #Output file

$ffCMD = cmd.exe /c "$ffmpeg $ffArgs"
$ffCMD = cmd.exe /c "`"$ffmpeg`" $ffArgs"

# Begin ffmpeg operation
$ffCMD
Expand Down
2 changes: 1 addition & 1 deletion files/func/GetAudioStreams.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Function GetAudioStreams {
$ffprobeArgs += "-of "
$ffprobeArgs += "compact=p=0:nk=1"

[int[]] $audioStreamArray = cmd.exe /c "$ffprobe $ffprobeArgs"
[int[]] $audioStreamArray = cmd.exe /c "`"$ffprobe`" $ffprobeArgs"

#If last channel is not stereo, create one
If ($audioStreamArray[$audioStreamArray.Length-1] -gt 2 ) {
Expand Down
2 changes: 1 addition & 1 deletion files/func/GetCodec.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Function GetCodec {
$ffprobeArgs += "default=noprint_wrappers=1:nokey=1 "
$ffprobeArgs += "`"$sourceFile`""

$ffprobeCMD = cmd.exe /c "$ffprobe $ffprobeArgs"
$ffprobeCMD = cmd.exe /c "`"$ffprobe`" $ffprobeArgs"

If ($DiscoverType -eq "Duration") {
#Test whether the ffprobe result was invalid - usually happens in files with corrupt encoding
Expand Down
2 changes: 1 addition & 1 deletion files/init/preflight.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ValidateHandbrakeCLIPath -Path $cfg.handbrakecli_bin_dir
Validatemedia_path -Path $cfg.media_path

#Validate OutPath
If ($cfg.use_out_path) {
If ($cfg.use_out_path -eq 'true') {
ValidateOutPath -Path $cfg.out_path
}

Expand Down
2 changes: 1 addition & 1 deletion files/prop/properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Version
version=4.1.2
version=4.2
platform=ps

#URLs
Expand Down

0 comments on commit 9605ca2

Please sign in to comment.