Add support for remote video

Created on 25 January 2024, 5 months ago
Updated 28 June 2024, about 13 hours ago

Problem/Motivation

I want to be able to link to a remote video file and show it in a video tag.

✨ Feature request
Status

Needs review

Version

1.8

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada paulsheldrake

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @paulsheldrake
  • πŸ‡¨πŸ‡¦Canada paulsheldrake

    paulsheldrake β†’ changed the visibility of the branch 3417063-add-support-remote-video to hidden.

  • Merge request !16Add remote video option β†’ (Open) created by paulsheldrake
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 5 months ago
    6 pass
  • πŸ‡¨πŸ‡¦Canada paulsheldrake

    This should be ready to go.

  • Status changed to Needs review 5 months ago
  • πŸ‡©πŸ‡°Denmark Steven Snedker

    Thank you!

    I can confirm it works with a short URL like https://vertikal.dk/sites/default/files/keys.mp4

    On URLs longer than 245 chars it breaks. Because the name of the video is supposed to fit in 255 chars.

    In the MediaRemoteVideoFormatter.php I changed

    public static function deriveMediaDefaultNameFromUrl($url) {
        $pattern = static::getUrlRegexPattern();
        if (preg_match($pattern, $url)) {
          return t('Video from @url', [
            '@url' => $url,
          ]);
        }
        return parent::deriveMediaDefaultNameFromUrl($url);
      }

    to

    public static function deriveMediaDefaultNameFromUrl($url) {
        $pattern = static::getUrlRegexPattern();
        if (preg_match($pattern, $url)) {
          return t('Video from @url', [
            '@url' => substr($url, 0, 200),
          ]);
        }
        return parent::deriveMediaDefaultNameFromUrl($url);
      }

    to make it work.

    I also had to relax getUrlRegexPattern() in order to make videos like
    http://mywonderfulNAS.myds.me:8764/share.cgi/Homevideo%20U-Matic/1061.mp...
    almost work.

    In the MediaRemoteVideoFormatter.php I changed

    public static function getUrlRegexPattern() {
        return '/^https?.*?\.mp4/';
      }
    

    to

    public static function getUrlRegexPattern() {
        return '/^http?.*/';
      }
    

    The player is still adamant that "No video with supported format and MIME type found" - but this is probably not related to this module or this formatter.

    At least the player will get the correct url this way.

  • πŸ‡©πŸ‡°Denmark Steven Snedker

    Having the video(url) show up in the media library added no benefits to my project, I found.

    So I ended up with a simpler solution than installing, patching, tweaking and setting up the Media Remote module:

    function mymodule_preprocess_node(&$variables) {
      if (!empty($variables['node']->get('field_video')->value)) {
        $video_link = $variables['node']->get('field_video')->value;
        $variables['content']['field_video'] = [
          '#type' => 'inline_template',
          '#title' => 'Video',
          '#template' => '<video controls><source src="{{ url }}" type="video/mp4"></video>',
          '#context' => ['url' => $video_link],
        ];
    }

    If you have a text field called video in a node, and you type in a remote video url, when editing the node, a video player will appear when viewing the node, instead of the text.

    Video urls should be https://. Otherwise your browser might not play the video and might give you a strange error message

    I hope someone will save some hours.

Production build 0.69.0 2024