Skip to content

Commit

Permalink
Add command line support
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Aug 15, 2023
1 parent 866b4d2 commit 4d72c2a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Convert and edit subtitles and captions.
| Comma separated values | .csv | csv |
| Plaintext | .txt | txt |

## Command line
Can be used from the command line to convert from .srt to .vtt
```
php subtitles.phar subtitles.srt subtitles.vtt
```

## Installation (supports PHP 8.1+)
```
composer require mantas-done/subtitles
Expand Down
Binary file added bin/subtitles.phar
Binary file not shown.
5 changes: 5 additions & 0 deletions build/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Build steps:

composer install --no-dev
php build.php
test if conversion works
31 changes: 31 additions & 0 deletions build/build.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

// Name of the PHAR archive
$pharFile = '../bin/subtitles.phar';
if (file_exists($pharFile)) {
unlink($pharFile);
}

// Create a new PHAR archive
$phar = new Phar($pharFile);

// Add files to the archive
$includes = [];
$includes[] = '../src/Code/';
$includes[] = '../src/cli.php';
$includes[] = '../src/Subtitles.php';
$includes[] = '../vendor/autoload.php';
$includes[] = '../vendor/composer/';
$includes_string = implode('|', $includes);
$includes_string = str_replace('.', '\.', $includes_string);
$includes_string = str_replace('/', '\\/', $includes_string);
$includes_string = "/$includes_string/";
$phar->buildFromDirectory(__DIR__ . '/../', $includes_string);

// Set the default entry point (your CLI script)
$phar->setDefaultStub('/src/cli.php');

// Make the PHAR archive executable
//chmod($pharFile, 0755);

echo "PHAR archive created: $pharFile\n";
20 changes: 20 additions & 0 deletions src/cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

require_once '../vendor/autoload.php';

if (!isset($argv[1])) {
die('No input file');
}
if (!isset($argv[2])) {
die('No output file');
}

try {
\Done\Subtitles\Subtitles::convert(realpath($argv[1]), $argv[2]);
} catch (\Done\Subtitles\Code\UserException $e) {
echo 'Error: ' . $e->getMessage();
die(2);
} catch (Exception $e) {
echo 'Exception: ' . $e->getMessage();
die(1);
}

0 comments on commit 4d72c2a

Please sign in to comment.