Debian Codegraph @

examples/images_videos/simple_video_editor/simple_video_editor.dpr

raw
1 {
2 Copyright 2008-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Simple example of loading a video file.
17 See README.md for details. }
18 program simple_video_editor;
19
20 { Console, not GUI, since loading files through ffmpeg will output some
21 info on stdout anyway. }
22 {$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}
23
24 uses SysUtils, Math,
25 CastleUtils, CastleWindow, CastleGLImages, CastleControls,
26 CastleVideos, CastleStringUtils, CastleMessages, CastleColors,
27 CastleParameters, CastleGLUtils, CastleVectors, Classes,
28 CastleTimeUtils, CastleKeysMouse, CastleUriUtils,
29 CastleUIControls, CastleRectangles;
30
31 var
32 Window: TCastleWindow;
33
34 Video: TVideo;
35 GLVideo: TGLVideo2D;
36 VideoUrl: String;
37
38 Time: TFloatTime;
39 TimePlaying: boolean = true;
40
41 MenuEdit: TMenu;
42 MenuTimeBackwards: TMenuItemChecked;
43 MenuRevert, MenuSave: TMenuItem;
44
45 procedure RemakeGLVideo;
46 begin
47 FreeAndNil(GLVideo);
48 GLVideo := TGLVideo2D.Create(Video);
49 end;
50
51 procedure Render(Container: TCastleContainer);
52 const
53 TimeBarHeight = 10;
54 TimeBarMargin = 2;
55
56 procedure DrawStatus;
57 var
58 S: string;
59 Strs: TStringList;
60 begin
61 Strs := TStringList.Create;
62 try
63 S := Format('Video time: %f', [Time]);
64 if not TimePlaying then
65 S := S + ' (paused)';
66 Strs.Append(S);
67
68 if Video.Loaded then
69 begin
70 Strs.Append(Format('Video URL: %s', [VideoUrl]));
71 Strs.Append(Format('Size: %d x %d', [Video.Width, Video.Height]));
72 Strs.Append(Format('Time: %f seconds (%d frames, with %f frames per second)',
73 [Video.TimeDuration, Video.Count, Video.FramesPerSecond]));
74 end else
75 Strs.Append('Video not loaded');
76
77 FallbackFont.PrintStrings(15,
78 Window.Height - FallbackFont.Height * Strs.Count - TimeBarHeight, Yellow,
79 Strs, false, 2);
80 finally FreeAndNil(Strs) end;
81 end;
82
83 begin
84 if Video.Loaded then
85 begin
86 GLVideo.DrawableImageFromTime(Time).Draw(0, 0);
87
88 { draw time of the video bar }
89 DrawRectangle(Rectangle(0, Window.Height - TimeBarHeight,
90 Window.Width, TimeBarHeight), Black);
91 DrawRectangle(Rectangle(TimeBarMargin, Window.Height - TimeBarHeight + TimeBarMargin,
92 Round(MapRange(Video.IndexFromTime(Time), 0, Video.Count - 1,
93 0, Window.Width - 2 * TimeBarMargin)), TimeBarHeight - 2 * TimeBarMargin),
94 Gray);
95 end;
96
97 DrawStatus;
98 end;
99
100 procedure Update(Container: TCastleContainer);
101 begin
102 if TimePlaying then
103 Time := Time + Window.Fps.SecondsPassed;
104 end;
105
106 procedure LoadVideo(const NewVideoUrl: String);
107 begin
108 try
109 Video.LoadFromFile(NewVideoUrl);
110 VideoUrl := NewVideoUrl;
111 Time := 0;
112 MenuEdit.Enabled := Video.Loaded;
113 MenuRevert.Enabled := Video.Loaded;
114 MenuSave.Enabled := Video.Loaded;
115 RemakeGLVideo;
116 except
117 on E: Exception do
118 MessageOk(Window, 'Loading of "' + VideoUrl + '" failed:' + NL + E.Message);
119 end;
120 end;
121
122 procedure SaveVideo(const NewVideoUrl: String);
123 begin
124 try
125 Video.SaveToFile(NewVideoUrl);
126 VideoUrl := NewVideoUrl;
127 except
128 on E: Exception do
129 MessageOk(Window, 'Saving of "' + NewVideoUrl + '" failed:' + NL + E.Message);
130 end;
131 end;
132
133 procedure Open(Container: TCastleContainer);
134 begin
135 if Parameters.High = 1 then
136 LoadVideo(Parameters[1])
137 else
138 LoadVideo('castle-data:/flame_seamless/@counter(4).png');
139 end;
140
141 procedure Close(Container: TCastleContainer);
142 begin
143 FreeAndNil(GLVideo);
144 end;
145
146 procedure MenuClick(Container: TCastleContainer; MenuItem: TMenuItem);
147 var
148 S: string;
149 I: Integer;
150 FadeFrames: Cardinal;
151 begin
152 case MenuItem.IntData of
153 10:
154 begin
155 S := VideoUrl;
156 if Window.FileDialog('Open file', S, true) then
157 LoadVideo(S);
158 end;
159 13:
160 begin
161 S := VideoUrl;
162 if Window.FileDialog('Save to file', S, false) then
163 SaveVideo(S);
164 end;
165 15:
166 begin
167 if Video.Loaded then
168 LoadVideo(VideoUrl);
169 end;
170 20: Window.Close;
171 110: TimePlaying := not TimePlaying;
172 120: Time := 0;
173 130:
174 begin
175 Video.TimeLoop := not Video.TimeLoop;
176 GLVideo.TimeLoop := Video.TimeLoop;
177 end;
178 140:
179 begin
180 Video.TimeBackwards := not Video.TimeBackwards;
181 GLVideo.TimeBackwards := Video.TimeBackwards;
182 end;
183
184 { Editing operations.
185 Should be implemented nicer, as a single procedure with
186 TImageFunction callback... too lazy to do this now. }
187
188 410: begin
189 Assert(Video.Loaded);
190 for I := 0 to Video.Count - 1 do
191 Video.Items[I].Grayscale;
192 RemakeGLVideo;
193 end;
194 420..422:
195 begin
196 Assert(Video.Loaded);
197 for I := 0 to Video.Count - 1 do
198 Video.Items[I].ConvertToChannelRGB(MenuItem.IntData - 420);
199 RemakeGLVideo;
200 end;
201 430..432:
202 begin
203 Assert(Video.Loaded);
204 for I := 0 to Video.Count - 1 do
205 Video.Items[I].StripToChannelRGB(MenuItem.IntData - 430);
206 RemakeGLVideo;
207 end;
208 440: begin
209 Assert(Video.Loaded);
210 for I := 0 to Video.Count - 1 do
211 Video.Items[I].FlipHorizontal;
212 RemakeGLVideo;
213 end;
214
215 445: begin
216 FadeFrames := Min(10, Video.Count div 2);
217 if MessageInputQueryCardinal(Window,
218 'How many frames to use for fading?', FadeFrames) then
219 begin
220 Video.FadeWithSelf(FadeFrames);
221 RemakeGLVideo;
222 end;
223 end;
224
225 450: begin
226 Video.MixWithSelfBackwards;
227 RemakeGLVideo;
228 { MixWithSelfBackwards changes TimeBackwards, we have to reflect
229 this in our menu item. }
230 MenuTimeBackwards.Checked := Video.TimeBackwards;
231 end;
232 end;
233 end;
234
235 function CreateMainMenu: TMenu;
236 var
237 M: TMenu;
238 begin
239 Result := TMenu.Create('Main menu');
240 M := TMenu.Create('_File');
241 M.Append(TMenuItem.Create('_Open ...', 10, CtrlO));
242 M.Append(TMenuSeparator.Create);
243 MenuSave := TMenuItem.Create('_Save As ...', 13);
244 MenuSave.Enabled := false;
245 M.Append(MenuSave);
246 MenuRevert := TMenuItem.Create('_Revert', 15);
247 MenuRevert.Enabled := false;
248 M.Append(MenuRevert);
249 M.Append(TMenuSeparator.Create);
250 M.Append(TMenuItem.Create('_Exit', 20, CtrlW));
251 Result.Append(M);
252 M := TMenu.Create('_Playback');
253 M.Append(TMenuItemChecked.Create('_Playing / Paused', 110, CtrlP,
254 TimePlaying, true));
255 M.Append(TMenuItem.Create('_Rewind to Beginning', 120, keyHome));
256 M.Append(TMenuSeparator.Create);
257 M.Append(TMenuItemChecked.Create('_Loop', 130,
258 Video.TimeLoop, true));
259 MenuTimeBackwards := TMenuItemChecked.Create('_Play Backwards after Playing Forward', 140,
260 Video.TimeBackwards, true);
261 M.Append(MenuTimeBackwards);
262 Result.Append(M);
263 M := TMenu.Create('_Edit');
264 MenuEdit := M;
265 MenuEdit.Enabled := false;
266 M.Append(TMenuItem.Create('_Fade with Self (Makes Video Loop Seamless) ...', 445));
267 M.Append(TMenuItem.Create('_Mix with Self Backwards (Makes Video Loop Seamless)', 450));
268 M.Append(TMenuSeparator.Create);
269 M.Append(TMenuItem.Create('_Grayscale', 410));
270 M.Append(TMenuSeparator.Create);
271 M.Append(TMenuItem.Create('Convert to _Red Channel', 420));
272 M.Append(TMenuItem.Create('Convert to Gr_een Channel', 421));
273 M.Append(TMenuItem.Create('Convert to _Blue Channel', 422));
274 M.Append(TMenuSeparator.Create);
275 M.Append(TMenuItem.Create('Strip to Red Channel', 430));
276 M.Append(TMenuItem.Create('Strip to Green Channel', 431));
277 M.Append(TMenuItem.Create('Strip to Blue Channel', 432));
278 M.Append(TMenuSeparator.Create);
279 M.Append(TMenuItem.Create('Mirror Horizontally', 440));
280 Result.Append(M);
281 end;
282
283 begin
284 LoadAnimatedGifs := true;
285
286 Window := TCastleWindow.Create(Application);
287
288 try
289 Application.MainWindow := Window;
290
291 Video := TVideo.Create;
292
293 { We will actually handle 1st param in Init. }
294 Parameters.CheckHighAtMost(1);
295
296 Window.SetDemoOptions(keyF11, #0, true);
297 Window.AutoRedisplay := true;
298 Window.MainMenu := CreateMainMenu;
299 Window.OnMenuClick := @MenuClick;
300 Window.OnOpen := @Open;
301 Window.OnClose := @Close;
302 Window.OnRender := @Render;
303 Window.OnUpdate := @Update;
304
305 Window.OpenAndRun;
306 finally
307 FreeAndNil(Video);
308 end;
309 end.