How to play a video?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By eaglecat
:warning: Old Version Published before Godot 3 was released.

Hello!
is it possible to load a video files(specifically, mpg and avi files) and play it?
tried to use VideoPlayer before, but I cant find out how to use it.

I think Godot can play Theora videos (.ogv) but I never tried it yet.

Zylann | 2016-05-28 11:07

:bust_in_silhouette: Reply From: Calinou

Godot can currently only decode Ogg Theora (file extension: .ogg or .ogv). Take a look at the VideoPlayer class.

You can encode videos for Ogg Theora using tools like FFmpeg.

Note that there is also OS.native-video-play() that should play mp4 (and some others) on Android and iPad devices.

SuperUserNameMan | 2016-08-24 14:17

are there any examples using the OS.native-video-play() function? I can not get this to work either?

ChildLearning.Club | 2022-10-20 04:16

:bust_in_silhouette: Reply From: SuperUserNameMan

If you only want to play fullscreen video with sound (and eventually subtitles), you can include ffplay from ffmpeg into a sub directory of your project, and run it using the OS.execute() function.

extends Control

var pid = null;

func _ready(): #{
	pass
#}
	
func play(videopath): #{
	pid = OS.execute("bin/ffplay",["-autoexit", "-exitonkeydown", "-exitonmousedown", "-fs", videopath ],true);	
#}

func _on_Play_pressed(): play("video/test.mp4");

According to my test, under Windows, specifying the .exe extension is not mandatory. This mean this code should work on all platforms.

Thanks, this is a great workaround.

SteveSmith | 2022-06-17 13:08

Does this still work? I think I have everything configured correctly, but still get the errors below?

extends Control

var pid = null;

func _ready(): #{
	pass
#}

func play(videopath): #{
	pid = OS.execute("res://bin/ffplay",["-autoexit", "-exitonkeydown", "-exitonmousedown", "-fs", videopath ],true); 
#}

func _on_Play_pressed(): play("res://assets/videos/P1010293.MP4");

Error from Godot Console:

E 0:00:01.658 execute: Condition “ret == 0” is true. Returned: ERR_CANT_FORK
<C++ Source> platform/windows/os_windows.cpp:2905 @ execute()
VideoControl.gd:10 @ play()
VideoControl.gd:13 @ _on_Play_pressed()


Error from Web Browser Dev Tools:

tmp_js_export.js:369 ERROR: OS::execute() must be implemented in Javascript via ‘engine.setOnExecute’ if required.

at: execute (platform/javascript/os_javascript.cpp:891) - Condition “failed” is true. Returned: ERR_UNAVAILABLE

ChildLearning.Club | 2022-10-20 04:07

Does it work for you in the editor?

SteveSmith | 2022-10-20 07:11

I didn’t get any errors before running the program, if that is what you are asking? It was not until after running it and hitting the connected _on_Play_pressed button.

ChildLearning.Club | 2022-10-20 07:18

It’s just that the error mentions Javascript, so I wondered if it was only failing after exporting.

SteveSmith | 2022-10-20 07:38

Yes, that is the error I get after the web export.

ChildLearning.Club | 2022-10-20 07:58

But does it work when running in the editor?

SteveSmith | 2022-10-20 08:07

I don’t get any video to display, or play, in the editor or otherwise.

ChildLearning.Club | 2022-10-20 08:08