Blog IT

The example above assumes that you have the video file ID. This could be the case when you are converting video fields to media. 

To do this you have to use the Remote video media type.

use Drupal\media\Entity\Media;

$video_media = Media::create([
  'bundle' => 'remote_video',
  'uid' => 1,
  'name' => 'DrupalCon Prague 2022 Driesnote',
   'field_media_oembed_video' => [
     'value' => 'https://www.youtube.com/watch?v=Hmit4ET-l3Q',
   ],
]);
$video_media->save();