Skip to content

Commit

Permalink
MDL-63609 media_videojs: support Youtube URL start time.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Apr 21, 2020
1 parent 788dfb9 commit 956f636
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
30 changes: 30 additions & 0 deletions media/player/videojs/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public function embed($urls, $name, $width, $height, $options) {
if ($this->youtube) {
$datasetup[] = '"techOrder": ["youtube"]';
$datasetup[] = '"sources": [{"type": "video/youtube", "src":"' . $urls[0] . '"}]';

// Check if we have a time parameter.
if ($time = $urls[0]->get_param('t')) {
$datasetup[] = '"youtube": {"start": "' . self::get_start_time($time) . '"}';
}

$sources = ''; // Do not specify <source> tags - it may confuse browser.
$isaudio = false; // Just in case.
} else if ($flashtech) {
Expand Down Expand Up @@ -218,6 +224,30 @@ protected static function pick_video_size(&$width, &$height) {
parent::pick_video_size($width, $height);
}

/**
* Method to convert Youtube time parameter string, which can contain human readable time
* intervals such as '1h5m', '1m10s', etc or a numeric seconds value
*
* @param string $timestr
* @return int
*/
protected static function get_start_time(string $timestr): int {
if (is_numeric($timestr)) {
// We can return the time string itself if it's already numeric.
return (int) $timestr;
}

try {
// Parse the time string as an ISO 8601 time interval.
$timeinterval = new DateInterval('PT' . core_text::strtoupper($timestr));

return ($timeinterval->h * HOURSECS) + ($timeinterval->i * MINSECS) + $timeinterval->s;
} catch (Exception $ex) {
// Invalid time interval.
return 0;
}
}

public function get_supported_extensions() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
Expand Down
49 changes: 35 additions & 14 deletions media/player/videojs/tests/player_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class media_videojs_testcase extends advanced_testcase {
class media_videojs_player_testcase extends advanced_testcase {

/**
* Pre-test setup. Preserves $CFG.
Expand Down Expand Up @@ -273,19 +273,6 @@ public function test_youtube() {
$this->youtube_plugin_engaged($t);
$this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);

// Format: youtube video with start time.
$url = new moodle_url('https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h11s');
$t = $manager->embed_url($url);
$this->youtube_plugin_engaged($t);
$this->assertContains('t=1h11s', $t);

// Format: youtube video within playlist with start time.
$url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0&t=1m5s');
$t = $manager->embed_url($url);
$this->youtube_plugin_engaged($t);
$this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);
$this->assertContains('t=1m5s', $t);

// Format: youtube playlist - not supported.
$url = new moodle_url('http://www.youtube.com/view_play_list?p=PL6E18E2927047B662');
$t = $manager->embed_url($url);
Expand All @@ -296,7 +283,41 @@ public function test_youtube() {
$url = new moodle_url('http://www.youtube.com/p/PL6E18E2927047B662');
$t = $manager->embed_url($url);
$this->assertNotContains('mediaplugin_videojs', $t);
}

/**
* Data provider for {@see test_youtube_start_time}
*
* @return array
*/
public function youtube_start_time_provider(): array {
return [
['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h11s', 3611],
['https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0&t=1m5s', 65],
['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h10m30s', 4230],
['https://www.youtube.com/watch?v=JNJMF1l3udM&t=3m', 180],
['https://www.youtube.com/watch?v=JNJMF1l3udM&t=43s', 43],
['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1234', 1234],
['https://www.youtube.com/watch?v=JNJMF1l3udM&t=invalid', 0],
];
}

/**
* Test Youtube video embedding with URL's containing start time interval
*
* @param string $url
* @param int $expectedstart
*
* @dataProvider youtube_start_time_provider
*/
public function test_youtube_start_time(string $url, int $expectedstart) {
set_config('youtube', 1, 'media_videojs');
set_config('useflash', 0, 'media_videojs');

$embedcode = core_media_manager::instance()->embed_url(new moodle_url($url));

$this->youtube_plugin_engaged($embedcode);
$this->assertContains("&quot;youtube&quot;: {&quot;start&quot;: &quot;{$expectedstart}&quot;}", $embedcode);
}

/**
Expand Down

0 comments on commit 956f636

Please sign in to comment.