Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow homarus to use faststart for video conversion #162

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions Homarus/src/Controller/HomarusController.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?php
namespace Islandora\Homarus\Controller;

use GuzzleHttp\Psr7\StreamWrapper;
use Islandora\Crayfish\Commons\CmdExecuteService;
use Islandora\Crayfish\Commons\ApixFedoraResourceRetriever;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* Class HomarusController
Expand Down Expand Up @@ -92,7 +89,7 @@ public function __construct(

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\StreamedResponse
* @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function convert(Request $request)
{
Expand All @@ -116,10 +113,13 @@ public function convert(Request $request)
$cmd_params = "";
if ($format == "mp4") {
jordandukart marked this conversation as resolved.
Show resolved Hide resolved
$cmd_params = " -vcodec libx264 -preset medium -acodec aac " .
"-strict -2 -ab 128k -ac 2 -async 1 -movflags " .
"frag_keyframe+empty_moov ";
"-strict -2 -ab 128k -ac 2 -async 1 -y -movflags " .
"faststart ";
}

$temp_file_path = __DIR__ . "/../../static/" . basename($source) . "." . $format;
$this->log->debug('Tempfile: ' . $temp_file_path);

// Arguments to ffmpeg command are sent as a custom header.
$args = $request->headers->get('X-Islandora-Args');

Expand All @@ -133,22 +133,35 @@ public function convert(Request $request)
400
);
}

// Add -loglevel error so large files can be processed.
$args .= ' -loglevel error';
$this->log->debug("X-Islandora-Args:", ['args' => $args]);
$token = $request->headers->get('Authorization');
$headers = "'Authorization: $token'";
$cmd_string = "$this->executable -headers $headers -i $source $args $cmd_params -f $format -";
$cmd_string = "$this->executable -headers $headers -i $source $args $cmd_params -f $format $temp_file_path";
$this->log->debug('Ffmpeg Command:', ['cmd' => $cmd_string]);

// Return response.
return $this->generateDerivativeResponse($cmd_string, $source, $temp_file_path, $content_type);
}

/**
* @param string $cmd_string
* @param string $source
* @param string $path
* @param string $content_type
* @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function generateDerivativeResponse(string $cmd_string, string $source, string $path, string $content_type)
{
try {
return new StreamedResponse(
$this->cmd->execute($cmd_string, $source),
$this->cmd->execute($cmd_string, $source);
return (new BinaryFileResponse(
jordandukart marked this conversation as resolved.
Show resolved Hide resolved
$path,
200,
['Content-Type' => $content_type]
);
))->deleteFileAfterSend(true);
jordandukart marked this conversation as resolved.
Show resolved Hide resolved
} catch (\RuntimeException $e) {
$this->log->error("RuntimeException:", ['exception' => $e]);
return new Response($e->getMessage(), 500);
Expand Down
31 changes: 22 additions & 9 deletions Homarus/tests/Islandora/Homarus/Tests/HomarusControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -219,15 +220,27 @@ private function getDefaultController()
$mock_service = $prophecy->reveal();

// Create a controller.
$controller = new HomarusController(
$mock_service,
$this->content_types,
$this->default_content_type,
'convert',
$this->prophesize(Logger::class)->reveal(),
$this->mime_to_format,
$this->default_format
);
$controller = $this->getMockBuilder(HomarusController::class)
->onlyMethods(['generateDerivativeResponse'])
->setConstructorArgs([
$mock_service,
$this->content_types,
$this->default_content_type,
'convert',
$this->prophesize(Logger::class)->reveal(),
$this->mime_to_format,
$this->default_format,
])
->getMock();

$controller->method('generateDerivativeResponse')
->will($this->returnCallback(function ($cmd_string, $source, $path, $content_type) {
return new BinaryFileResponse(
__DIR__ . "/../../../fixtures/foo.mp4",
200,
['Content-Type' => $content_type]
);
}));
return $controller;
}

Expand Down
Empty file added Homarus/tests/fixtures/foo.mp4
Empty file.