Lire des vidéos

Godot supporte la lecture de vidéos avec le nœud VideoPlayer.

Supported playback formats

The only supported format in core is Ogg Theora (not to be confused with Ogg Vorbis audio). It's possible for extensions to bring support for additional formats, but no such extensions exist yet as of July 2022.

H.264 and H.265 cannot be supported in core Godot, as they are both encumbered by software patents. AV1 is royalty-free, but it remains slow to decode on the CPU and hardware decoding support isn't readily available on all GPUs in use yet.

WebM is supported in core in Godot 3.x, but support for it will be removed in 4.0 as it proved to be too buggy and difficult to maintain. Therefore, using WebM is not recommended.

Note

Vous pouvez trouver des vidéos avec une extension .ogg ou .ogx, qui sont des extensions génériques pour les données d'un conteneur Ogg.

Renommer ces extensions de fichiers en .ogv peut permettre ces vidéos d'être importées dans Godot. Cependant, tous les fichiers avec des extensions .ogg ou ``.ogx``ne sont pas des vidéos - certaines d'entre elles peuvent ne contenir que de l'audio.

Setting up VideoPlayer

  1. Create a VideoPlayer node using the Create New Node dialog.

  2. Select the VideoPlayer node in the scene tree dock, go to the inspector and load an .ogv file in the Stream property.

  3. Si vous voulez que la vidéo soit jouée dès que la scène est chargée, cochez Autoplay dans l'inspecteur. Si non, laissez Autoplay désactivé et appelez play() dans un script du nœud VideoPlayer pour commencer la lecture quand vous le désirez.

Handling resizing and different aspect ratios

By default in Godot 4.0, the VideoPlayer will automatically be resized to match the video's resolution. You can make it follow usual Control sizing by enabling Expand on the VideoPlayer node.

To adjust how the VideoPlayer node resizes depending on window size, adjust the anchors using the Layout menu at the top of the 2D editor viewport. However, this setup may not be powerful enough to handle all use cases, such as playing fullscreen videos without distorting the video (but with empty space on the edges instead). For more control, you can use an AspectRatioContainer node, which is designed to handle this kind of use case:

Add an AspectRatioContainer node. Make sure it is not a child of any other container node. Select the AspectRatioContainer node, then set its Layout at the top of the 2D editor to Full Rect. Set Ratio in the AspectRatioContainer node to match your video's aspect ratio. You can use math formulas in the inspector to help yourself. Remember to make one of the operands a float. Otherwise, the division's result will always be an integer.

La propriété Ratio de l'AspectRatioContainer est en cours de modification dans l'inspecteur de l'éditeur

Ce sera évalué à (approximativement) 1.777778

Once you've configured the AspectRatioContainer, reparent your VideoPlayer node to be a child of the AspectRatioContainer node. Make sure Expand is disabled on the VideoPlayer. Your video should now scale automatically to fit the whole screen while avoiding distortion.

Voir aussi

See Résolutions multiples for more tips on supporting multiple aspect ratios in your project.

Afficher une vidéo sur une surface 3D

En utilisant un noeud VideoPlayer comme enfant d'un noeud Viewport, il est possible d'afficher n'importe quel noeud 2D sur une surface 3D. Par exemple, cela peut être utilisé pour afficher des panneaux publicitaires animés quand l'animation trame par trame nécessiterait trop de mémoire.

This can be done with the following steps:

  1. Créez un noeud Viewport. Réglez sa taille pour qu'il corresponde à la taille de votre vidéo en pixels.

  2. Créez un nœud VideoPlayer en tant qu'enfant du nœud Viewport et spécifiez lui le chemin de la vidéo. Vérifiez que Expand est désactivé, et activez Autoplay si besoin.

  3. Créez un nœud MeshInstance avec une ressource PlaneMesh ou QuadMesh dans la propriété Mesh. Redimensionnez le maillage pour qu'il corresponde au format de la vidéo (sinon, celle-ci apparaîtra déformée).

  4. Créez une nouvelle ressource SpatialRessource dans la propriété Material Override de la section GeometryInstance.

  5. Enable Local To Scene in the SpatialMaterial's Resource section (at the bottom). This is required before you can use a ViewportTexture in its Albedo Texture property.

  6. Dans le SpatialMaterial, définissez la propriété Albedo > Texture à New ViewportTexture. Editez la nouvelle ressource en cliquant dessus, puis spécifiez le chemin vers le nœud Viewport dans la propriété Viewport Path.

  7. Activez Albedo Tex Force sRGB dans le SpatialMaterial pour éviter que les couleurs ne soient délavées.

  8. Si le panneau d'affichage est censé émettre sa propre lumière, activez Flags > Unshaded pour améliorer les performances de rendu.

See Utilisation de Viewports and the GUI in 3D demo for more information on setting this up.

Playback limitations

L'implémentation actuelle de la lecture vidéo dans Godot présente plusieurs limitations :

  • Lancer la vidéo à un moment précis n'est pas pris en charge.

  • Changing playback speed is not supported. VideoPlayer also won't follow Engine.time_scale.

  • Looping is not supported, but you can connect a VideoPlayer's finished signal to a function that plays the video again. However, this will cause a black frame to be visible when the video restarts. This can be worked around by adding a fade to black in the video file before the video ends, or by hiding the video for one frame and displaying a TextureRect with a screenshot of the first frame of the video until the video is restarted.

  • Le streaming d'une vidéo à partir d'une URL n'est pas prise en charge.