-
Notifications
You must be signed in to change notification settings - Fork 0
/
renameVids.php
executable file
·99 lines (92 loc) · 2.28 KB
/
renameVids.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?
//$base_dir = "/backup/Sync/Videos";
$base_dir = "/chell/d/Videos";
$folders = glob($base_dir."/*");
foreach($folders as $folder)
{
if(is_dir($folder))
{
$folder_parts = explode("/", $folder);
$show = end($folder_parts);
// echo "Show: ".$show."\n";
$files = getFiles($folder);
renameFiles($files, $show);
}
}
function getFiles($dir)
{
$files = glob($dir."/*");
return $files;
}
function renameFiles($files, $show, $season)
{
foreach($files as $file)
{
if(is_dir($file))
{
$folder_parts = explode("/", $file);
$season = end($folder_parts);
// echo "\tSeason: ".$season."\n";
$file_list = getFiles($file);
renameFiles($file_list, $show, $season);
}
else
{
renameFile($file, $show, $season);
}
}
}
function renameFile($file, $show, $season)
{
global $base_dir;
$file_parts = explode("/", $file);
$filename = end($file_parts);
$filename_parts = explode(".", $filename);
$ext = end($filename_parts);
if($filename == "Thumbs.db")
{
unlink($filename);
echo "Deleting ".$file."\n";
}
$season_pattern = "S[0-9]{2}";
$episode_pattern = "E[0-9]{2}";
$full_pattern = $show." - ".$season_pattern.$episode_pattern;
if(!preg_match("/".$full_pattern."/", $filename, $matches))
{
unset($matches);
if(preg_match("/".$season_pattern.$episode_pattern."/", $filename, $matches))
{
$full_episode = $matches[0];
if(empty($season))
{
$season = "Season ".ltrim(substr($full_episode, 1, 2), '0');
}
$new_file = $base_dir."/".$show."/".$season."/".$show." - ".$full_episode.".".$ext;
}
else
{
$filename = str_replace(".", " ", $filename);
$filename_parts = explode(" ", $filename);
$ext = end($filename_parts);
$full_episode = $filename_parts[0];
if(strlen($filename_parts[0]) == 4)
{
$full_episode = "S".substr($full_episode, 0, 2)."E".substr($full_episode, 2, 2);
}
else
{
$full_episode = "S0".$full_episode[0]."E".substr($full_episode, 1, 2);
}
$new_file = $base_dir."/".$show."/".$season."/".$show." - ".$full_episode.".".$ext;
}
if(isset($new_file) && !empty($new_file))
{
$file = str_replace($filename, '"'.$filename.'"', $file);
$new_file = addslashes($new_file);
$cmd = "mv ".str_replace(" ", "\ ", $file)." ".str_replace(" ", "\ ", $new_file);
echo $cmd."\n";
exec($cmd);
}
}
}
?>