Jump to content

Segregate Movies


Nologic

Recommended Posts

Nologic

Why?

My brother was a bit messy with his encodes, such as mixing Bluray with DVD, along with encodes done in ether H264 or Xvid. This moves things around, so its easier to figure out if something should be ripped again in H264 rather than leaving it in Xvid...or replace DVD rips with Blurays that are now available.

 

Notes:

The Folders this script is to run against must be on ether a local or network mapped drive, as the script isn't setup to handle UNC paths.

 

Requirements:

AutoIt

FFMpeg

 

Instructions:

Install AutoIt and set it to execute scripts when double clicked.

 

Create a folder somewhere...lets call it "Segregate Movies" just to be unimaginative.

 

Now inside "Segregate Movies" create a sub folder called "Bin".

 

Now extract the contents of FFMpeg's "Bin" folder into your new "Bin" sub folder.

 

Now going back into "Segregate Movies" folder Right Click on it's background and select "New\AutoIt v3 Script" from the context menu, and Rename it "Segregate Movies.au3"

 

Now Right Click "Segregate Movies.au3" & select "Edit Script" from the context menu.

 

Now paste the following code into:

 

 

#Include <Array.au3>
#Include <Constants.au3>
#Include <File.au3>
#Include <Process.au3>
#Include <String.au3>

;================================================================================
;== User Vars
;================================================================================

$sFFMpegPath = 'F:\My Projects\Segregate Movies\BIN\'

;================================================================================
;================================================================================

Const $cExtensions = '*.3gp;*.avi;*.divx;*.f4v;*.flv;*.m2ts;*.m4v;*.mkv;*.mp4;*.mpg;*.ogg;*.ogm;*.ts;*.vob'

; Select Source Folder
While 1
	$sSource = FileSelectFolder( 'Select Source Folder.' , '' , 6 )
	If @[member="ERROR"] = 1 AND $sSource = '' Then
		MsgBox( 4096 , 'Exit:' , 'Ending Application' )
		Exit
	ElseIf $sSource = '' Then
		MsgBox( 4096 , 'Error:' , 'Unable To Open Folder.' )
	ElseIf NOT FileExists( $sSource ) Then
		MsgBox( 4096 , 'Error:' , 'Invalid Folder.' )
	Else
		If StringRight( $sSource , 1 ) <> '\' Then $sSource &= '\'
		ExitLoop
	EndIf
WEnd

; Select Final Folder
While 1
	$sFinal = FileSelectFolder( 'Select Final Folder.' , '' , 6 )
	If @[member="ERROR"] = 1 AND $sFinal = '' Then
		MsgBox( 4096 , 'Exit:' , 'Ending Application' )
		Exit
	ElseIf $sFinal = '' Then
		MsgBox( 4096 , 'Error:' , 'Unable To Open Folder.' )
	ElseIf NOT FileExists( $sFinal ) Then
		MsgBox( 4096 , 'Error:' , 'Invalid Folder.' )
	Else
		If StringRight( $sFinal , 1 ) <> '\' Then $sFinal &= '\'
		ExitLoop
	EndIf
WEnd

$aAllFiles = _FileListToArrayRec( $sSource , $cExtensions , 1 , 1 , 0 , 2 )
If IsArray( $aAllFiles ) Then
	For $ii = 1 To $aAllFiles[0]
		_Macros( $aAllFiles[$ii] )

		; Build Array of Streams 1
		$sRandomString = _RandomString()
		RunWait( @ComSpec & ' /c ffprobe.exe -show_streams "' & $aAllFiles[$ii] & '" >"' & @TempDir & '\' & $sRandomString & '.txt"' , $sFFMpegPath , @SW_HIDE )
		; Build Array of Streams 2
		$aAllStreams = _StringBetween( FileRead( @TempDir & '\' & $sRandomString & '.txt' ) , '[STREAM]' , '[/STREAM]' )
		FileDelete( @TempDir & '\' & $sRandomString & '.txt' )

		; Create Stream Count for All Streams
		$iAllStreamsCount = UBound( $aAllStreams )

		; Find Video Streams
		$aFindVideoStreams = _ArrayFindAll( $aAllStreams , 'codec_type=video' , 0 , 0 , 0 , 1 )
		If $aFindVideoStreams <> -1 Then
			; Process Video Streams
			For $aa = 0 To UBound( $aFindVideoStreams ) - 1
				$sSVideoCodec = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?codec_name=(\S+))?' )
				$iSVideoWidth = _RegExString( $aAllStreams[$aFindVideoStreams[$aa]] , '(?s)(?i)(?:.+?width=(\S+))?'      )
				FileSetAttrib( $sFileDir , '-RASHNOT' , 1 )
				FileSetAttrib( $sFileDir & '*' , '-RASHNOT' , 1 )
				Select
					Case $iSVideoWidth > 2048
						$sRez = '4K'
					Case $iSVideoWidth > 1920
						$sRez = '2K'
					Case $iSVideoWidth > 1280
						$sRez = '1080p'
					Case $iSVideoWidth >  720
						$sRez = '720p'
					Case $iSVideoWidth >= 640
						$sRez = 'DVD'
					Case Else
						$sRez = 'Junk'
				EndSelect
				DirCreate( $sFinal & '_' & $sRez & '_' & _CleanFileName( $sSVideoCodec ) & '\' )
				DirMove( $sFileDir , $sFinal & '_' & $sRez & '_' & _CleanFileName( $sSVideoCodec ) & '\' & $sParent , 1 )
			Next
		EndIf
	Next
EndIf

MsgBox ( 0 , 'Finished:' , 'Movie Segregation Complete.' )




Func _CleanFileName( $sFileName )
    $sFileName = StringReplace( $sFileName , '\' , '-' )
    $sFileName = StringReplace( $sFileName , '|' , '-' )
    $sFileName = StringReplace( $sFileName , '/' , '-' )
    $sFileName = StringReplace( $sFileName , ':' , '-' )
    $sFileName = StringReplace( $sFileName , '"' , "'" )
    $sFileName = StringReplace( $sFileName , '*' , ' ' )
    $sFileName = StringReplace( $sFileName , '?' , ''  )
    $sFileName = StringReplace( $sFileName , '<' , ''  )
    $sFileName = StringReplace( $sFileName , '>' , ''  )
    Return $sFileName
EndFunc


Func _Macros( $sFile )
	Global $sFileDir    = StringLeft     ( $sFile     , StringInStr( $sFile     , '\' , 0 , -1 ) )
	Global $sFileExt    = StringTrimLeft ( $sFile     , StringInStr( $sFile     , '.' , 0 , -1 ) - 1 )
	Global $sFileName   = StringTrimLeft ( $sFile     , StringInStr( $sFile     , '\' , 0 , -1 ) )
	Global $sShortName  = StringLeft     ( $sFileName , StringInStr( $sFileName , '.' , 0 , -1 ) - 1 )
	Global $sParent     = StringTrimLeft ( $sFileDir  , StringInStr( $sFileDir  , '\' , 0 , -2 ) )
	Global $sFolderName = StringTrimRight( StringTrimLeft( $sFileDir , StringInStr( $sFileDir , '\' , 0 , -2 ) ) , 1 )
EndFunc

Func _RandomString()
	Local $sString , $aSpace[3]

	For $i = 1 To 15
		$aSpace[0] = Chr( Random( 65 ,  90 , 1 )) ; A-Z
		$aSpace[1] = Chr( Random( 97 , 122 , 1 )) ; a-z
		$aSpace[2] = Chr( Random( 48 ,  57 , 1 )) ; 0-9
		$sString &= $aSpace[Random( 0 , 2 , 1 )]
	Next

	Return $sString
EndFunc

Func _RegExString( $sString , $sRegEx )
	Local $sValue = _ArrayToString( StringRegExp( $sString , $sRegEx , 1 ))
	If StringIsFloat( $sValue ) = 1 OR StringIsInt( $sValue ) = 1 Then
		Return Number( $sValue )
	Else
		If $sValue = 'N/A' OR $sValue = 'N' OR $sValue = 'A' Then
			Return ''
		Else
			Return $sValue
		EndIf
	EndIf
EndFunc

 

 

 

Now you'll have to edit a User Var namely the path to FFMpeg...normally I'd have this set automatically to the Bin folder holding the script...but I'm getting a lot of scripts depending on FFMpeg...so I'm allowing you to set the path...so you really only have 1 copy on your system...or at least 1 less do to this script allowing you to set it to a different location.

 

Now save the script & execute it by double clicking it.

 

You will be asked for the "Source Folder"...this should be your main "Movies" folder...ie it should hold a collection of movies each in sub folders.

 

Next you will be asked for the "Final Folder"...this should ideally be on the same hard drive as the "Source Folder", to make things much quicker.

 

Once done you'll be able to look into your "Final Folder" and see sub folders labeled by resolution & codec...example _1080p_H264

Edited by Nologic
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...