File size: 4,536 Bytes
a7d70cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
-- PostcardVideoPlayer.client.lua
-- Colocar en: StarterPlayer > StarterPlayerScripts
--
-- Cuando recibe el aviso del servidor (al tocar PanelPostcard), pide al
-- servidor local (http://localhost:4000, definido en server.js) la info del
-- video y sus frames, y los va pintando en un EditableImage a pantalla
-- completa, frame a frame, respetando los fps del video.
--
-- IMPORTANTE:
--  * Hay que activar "Allow HTTP Requests" en Game Settings > Security.
--  * Esto solo funciona probando en Roblox Studio (Play / Play Solo), porque
--    "localhost" es tu propio ordenador, no llega a un juego ya publicado.
--  * Requiere una versión de Roblox con soporte de EditableImage / Content.

local HttpService = game:GetService("HttpService")
local AssetService = game:GetService("AssetService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local BASE_URL = "http://localhost:4000"

local remoteEvent = ReplicatedStorage:WaitForChild("PlayPostcardVideoEvent")
local player = Players.LocalPlayer

local isPlaying = false

local function buildGui()
	local screenGui = Instance.new("ScreenGui")
	screenGui.Name = "PostcardVideoGui"
	screenGui.IgnoreGuiInset = true
	screenGui.DisplayOrder = 999
	screenGui.ResetOnSpawn = false
	screenGui.Enabled = false

	local background = Instance.new("Frame")
	background.Name = "Background"
	background.Size = UDim2.new(1, 0, 1, 0)
	background.Position = UDim2.new(0, 0, 0, 0)
	background.BackgroundColor3 = Color3.new(0, 0, 0)
	background.BorderSizePixel = 0
	background.ZIndex = 1
	background.Parent = screenGui

	local imageLabel = Instance.new("ImageLabel")
	imageLabel.Name = "VideoFrame"
	imageLabel.Size = UDim2.new(1, 0, 1, 0)
	imageLabel.Position = UDim2.new(0, 0, 0, 0)
	imageLabel.BackgroundTransparency = 1
	imageLabel.ScaleType = Enum.ScaleType.Fit
	imageLabel.ZIndex = 2
	imageLabel.Parent = background

	local closeButton = Instance.new("TextButton")
	closeButton.Name = "CloseButton"
	closeButton.Size = UDim2.new(0, 44, 0, 44)
	closeButton.Position = UDim2.new(1, -54, 0, 10)
	closeButton.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
	closeButton.BackgroundTransparency = 0.3
	closeButton.Text = "X"
	closeButton.TextColor3 = Color3.new(1, 1, 1)
	closeButton.TextScaled = true
	closeButton.Font = Enum.Font.GothamBold
	closeButton.ZIndex = 3
	closeButton.Parent = background

	screenGui.Parent = player:WaitForChild("PlayerGui")

	return screenGui, imageLabel, closeButton
end

local screenGui, imageLabel, closeButton = buildGui()

local function stopPlayback()
	isPlaying = false
	screenGui.Enabled = false
end

closeButton.MouseButton1Click:Connect(stopPlayback)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	if input.KeyCode == Enum.KeyCode.Escape and screenGui.Enabled then
		stopPlayback()
	end
end)

local function httpGet(url)
	local ok, result = pcall(function()
		return HttpService:RequestAsync({
			Url = url,
			Method = "GET",
		})
	end)
	if not ok or not result or not result.Success then
		return nil
	end
	return result
end

local function playVideo()
	if isPlaying then
		return
	end

	local infoResponse = httpGet(BASE_URL .. "/video/info")
	if not infoResponse then
		warn("No se pudo conectar con el servidor de video en " .. BASE_URL .. " (¿está corriendo 'npm start' en la carpeta Uidevideo?)")
		return
	end

	local ok, info = pcall(function()
		return HttpService:JSONDecode(infoResponse.Body)
	end)
	if not ok or not info or not info.ready then
		warn("El servidor de video todavía no está listo (¿terminó de procesar video.mp4?)")
		return
	end

	local width = info.width
	local height = info.height
	local fps = info.fps
	local frameCount = info.frameCount
	local frameTime = 1 / fps

	local editableImage = AssetService:CreateEditableImage({
		Size = Vector2.new(width, height),
	})
	imageLabel.ImageContent = Content.fromObject(editableImage)

	isPlaying = true
	screenGui.Enabled = true

	for i = 0, frameCount - 1 do
		if not isPlaying then
			break
		end

		local frameResponse = httpGet(BASE_URL .. "/video/frame/" .. i)
		if frameResponse and isPlaying then
			local bytes = frameResponse.Body
			local buf = buffer.fromstring(bytes)
			editableImage:WritePixelsBuffer(Vector2.new(0, 0), Vector2.new(width, height), buf)
		end

		task.wait(frameTime)
	end

	if isPlaying then
		stopPlayback()
	end
end

remoteEvent.OnClientEvent:Connect(function()
	playVideo()
end)