Last active
March 25, 2019 19:51
-
-
Save bantya/ebd1c397dad0712c4ec29691bee69ec2 to your computer and use it in GitHub Desktop.
VLC playlist creator script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# @created 2019-03-24 | |
class CreatePlaylist | |
{ | |
protected $xml; | |
protected $files; | |
protected $content; | |
public function __construct(array $files) | |
{ | |
$this->xml = new DOMDocument('1.0', 'UTF-8'); | |
$this->files = $files; | |
$this->content = $this->generateMarkup(); | |
} | |
public function __destruct() | |
{ | |
$this->xml = null; | |
$this->files = null; | |
$this->content = null; | |
} | |
public function saveFile(string $filepath) | |
{ | |
return $this->xml->save($filepath); | |
} | |
private function generateMarkup() { | |
$playlist = $this->setAttributes($this->xml->createElement('playlist'), [ | |
'xmlns' => 'http://xspf.org/ns/0/', | |
'xmlns:vlc' => 'http://www.videolan.org/vlc/playlist/ns/0/', | |
'version' => '1', | |
]); | |
$playlist->appendChild($this->xml->createElement('title', 'Playlist')); | |
$playlist = $this->xml->appendChild($playlist); | |
$tracklist = $this->xml->createElement('trackList'); | |
$extension = $this->setAttributes($this->xml->createElement('extension'), [ | |
'application' => 'http://www.videolan.org/vlc/playlist/0', | |
]); | |
foreach ($this->files as $id => $value) { | |
$track = $this->xml->createElement('track'); | |
$location = $this->xml->createElement('location', 'file:///' . $value); | |
$duration = $this->xml->createElement('duration', $id); | |
$track = $this->appendChildrens($track, $location, $duration); | |
$ext = $this->setAttributes($this->xml->createElement('extension'), [ | |
'application' => 'http://www.videolan.org/vlc/playlist/0' | |
]); | |
$ext->appendChild($this->xml->createElement('vlc:id', $id)); | |
$track->appendChild($ext); | |
$extension->appendChild($this->setAttributes($this->xml->createElement('vlc:item'), [ | |
'tid' => $id | |
])); | |
$tracklist->appendChild($track); | |
} | |
$playlist = $this->appendChildrens($playlist, $tracklist, $extension); | |
$this->xml->formatOutput = true; | |
} | |
private function setAttributes(DOMElement $element, array $attributes) : DOMElement { | |
foreach ($attributes as $attribute => $value) { | |
$element->setAttribute($attribute, $value); | |
} | |
return $element; | |
} | |
private function appendChildrens(DOMNode $node, ...$elements) : DOMNode { | |
foreach ((array) $elements as $key => $element) { | |
$node->appendChild($element); | |
} | |
return $node; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "CreatePlaylist.php"; | |
$downloadsPath = joinPath(getenv('USERPROFILE'), 'Downloads'); | |
# @created 2019-03-24 Created and added ability to use from anywhere | |
$directory = rtrim($argv[1], DIRECTORY_SEPARATOR); | |
$parts = explode(DIRECTORY_SEPARATOR, $directory); | |
$name = strtolower(end($parts)); | |
$name = str_replace(' ', '-', $name); | |
$lookFor = [ | |
'mp4', | |
'webm', | |
]; | |
if (! preg_match('/^[a-zA-Z]\:\\\/', $directory) && joinPath($downloadsPath, $directory)) { | |
$directory = joinPath($downloadsPath, $directory); | |
} | |
if (! is_dir($directory)) { | |
throw new Exception("Invalid directory given.", 1); | |
} else if (! is_readable($directory)) { | |
throw new Exception("The directory content is not readable.", 1); | |
} | |
$files = getFiles($directory, $lookFor); | |
# Generate and save file. | |
$result = (new CreatePlaylist($files))->saveFile( | |
generateName($name) | |
); | |
if ($result) { | |
echo PHP_EOL . "\e[32m✔\e[0m \e[2;36m{$name}\e[0m saved successfully!" . PHP_EOL; | |
} else { | |
echo PHP_EOL . "\e[31m❌\e[0m \e[2;36m{$name}\e[0m save failed!" . PHP_EOL; | |
} | |
function getFiles(string $directory, $lookFor) { | |
return array_values( | |
array_filter( | |
array_map( | |
function ($file) use ($lookFor, $directory) { | |
if ($file === '.' || $file === '..') { | |
return false; | |
} | |
if (! in_array(getExtension($file), $lookFor)) { | |
return false; | |
} | |
$file = joinPath($directory, $file); | |
return str_replace( | |
[' ', '!', '"', '#', '$', '&', "'", '(', ')', '*', '+', ',', ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '\\'], | |
['%20', '%21', '%22', '%23', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3C', '%3D', '%3E', '%3F', '%40', '%5B', '%5D', '%5E', '%5F', '%60', '%7B', '%7C', '%7D', '%7E', '/'], | |
$file); | |
}, | |
scandir($directory) | |
) | |
) | |
); | |
} | |
function generateName(string $name) { | |
$realpath = trim(trim(realpath(__FILE__), 'playlist.php'), DIRECTORY_SEPARATOR); | |
return joinPath($realpath, 'files', $name . '.xspf'); | |
} | |
function getExtension(string $file) { | |
return pathinfo($file, PATHINFO_EXTENSION); | |
} | |
function joinPath() { | |
return implode(DIRECTORY_SEPARATOR, func_get_args()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php playlist.php [VIDEOS_DIRECTORY] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment