Skip to content

Commit 17d13e6

Browse files
committed
### Added
- Use `php-shellcommand` to allow for proper execution on Windows & Unix servers ### Changed - Minor code cleanup
1 parent 0b212f4 commit 17d13e6

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Transcoder Changelog
22

3+
## 1.0.3 - 2017.03.11
4+
### Added
5+
- Use `php-shellcommand` to allow for proper execution on Windows & Unix servers
6+
7+
### Changed
8+
- Minor code cleanup
9+
310
## 1.0.2 - 2017.03.07
411
### Added
512
- Added a summary option to `getFileInfo()`

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nystudio107/craft3-transcoder",
33
"description": "Transcode video & audio files to various formats, and provide video thumbnails",
44
"type": "craft-plugin",
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"keywords": [
77
"craft",
88
"cms",
@@ -22,6 +22,7 @@
2222
}
2323
],
2424
"require": {
25+
"mikehaertl/php-shellcommand": "~1.2"
2526
},
2627
"autoload": {
2728
"psr-4": {

src/services/Transcoder.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
use yii\base\Exception;
1919

20+
use mikehaertl\shellcommand\Command as ShellCommand;
21+
2022
/**
2123
* @author nystudio107
2224
* @package Transcoder
@@ -178,8 +180,8 @@ public function getVideoUrl($filePath, $videoOptions): string
178180
$result = Craft::$app->config->get("transcoderUrl", "transcoder").$destVideoFile;
179181
} else {
180182
// Kick off the transcoding
181-
$pid = shell_exec($ffmpegCmd);
182-
Craft::info($ffmpegCmd, __METHOD__);
183+
$pid = $this->_executeShellCommand($ffmpegCmd);
184+
Craft::info($ffmpegCmd."\nffmpeg PID: ".$pid, __METHOD__);
183185

184186
// Create a lockfile in tmp
185187
file_put_contents($lockFile, $pid);
@@ -239,7 +241,7 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions): string
239241

240242
// If the thumbnail file already exists, return it. Otherwise, generate it and return it
241243
if (!file_exists($destThumbnailPath)) {
242-
$shellOutput = shell_exec($ffmpegCmd);
244+
$shellOutput = $this->_executeShellCommand($ffmpegCmd);
243245
Craft::info($ffmpegCmd, __METHOD__);
244246
}
245247
$result = Craft::$app->config->get("transcoderUrl", "transcoder").$destThumbnailFile;
@@ -252,7 +254,7 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions): string
252254
* Returns a URL to the transcoded audio file or "" if it doesn't exist
253255
* (at which time it will create it).
254256
*
255-
* @param $filePath path to the original audio file -OR- an Asset
257+
* @param $filePath string path to the original audio file -OR- an Asset
256258
* @param $audioOptions array of options for the audio file
257259
*
258260
* @return string URL of the transcoded audio file or ""
@@ -332,8 +334,8 @@ public function getAudioUrl($filePath, $audioOptions): string
332334
$result = Craft::$app->config->get("transcoderUrl", "transcoder").$destAudioFile;
333335
} else {
334336
// Kick off the transcoding
335-
$pid = shell_exec($ffmpegCmd);
336-
Craft::info($ffmpegCmd, __METHOD__);
337+
$pid = $this->_executeShellCommand($ffmpegCmd);
338+
Craft::info($ffmpegCmd."\nffmpeg PID: ".$pid, __METHOD__);
337339

338340
// Create a lockfile in tmp
339341
file_put_contents($lockFile, $pid);
@@ -364,7 +366,7 @@ public function getFileInfo($filePath, $summary = false): array
364366
.' '.$ffprobeOptions
365367
.' '.escapeshellarg($filePath);
366368

367-
$shellOutput = shell_exec($ffprobeCmd);
369+
$shellOutput = $this->_executeShellCommand($ffprobeCmd);
368370
Craft::info($ffprobeCmd, __METHOD__);
369371
$result = json_decode($shellOutput, true);
370372
Craft::info(print_r($result, true), __METHOD__);
@@ -600,4 +602,35 @@ protected function coalesceOptions($defaultName, $options): array
600602

601603
return $options;
602604
}
605+
606+
// Private Methods
607+
// =========================================================================
608+
609+
/**
610+
* Execute a shell command
611+
*
612+
* @param string $command
613+
*
614+
* @return string
615+
*/
616+
private function _executeShellCommand(string $command): string
617+
{
618+
// Create the shell command
619+
$shellCommand = new ShellCommand();
620+
$shellCommand->setCommand($command);
621+
622+
// If we don't have proc_open, maybe we've got exec
623+
if (!function_exists('proc_open') && function_exists('exec')) {
624+
$shellCommand->useExec = true;
625+
}
626+
627+
// Return the result of the command's output or error
628+
if ($shellCommand->execute()) {
629+
$result = $shellCommand->getOutput();
630+
} else {
631+
$result = $shellCommand->getError();
632+
}
633+
634+
return $result;
635+
}
603636
}

0 commit comments

Comments
 (0)