1
1
use std:: {
2
- fs:: { self , File } ,
3
- io:: Write ,
2
+ fs:: { self } ,
4
3
path:: PathBuf ,
5
- process:: { Command , Stdio } ,
6
- thread:: { self , Thread } ,
4
+ thread:: { self } ,
7
5
time:: Duration ,
8
6
} ;
9
7
10
8
use anyhow:: Result ;
11
- use compositor_api :: types :: Resolution ;
9
+ use compositor_render :: { event_handler :: subscribe , Resolution } ;
12
10
use generate:: compositor_instance:: CompositorInstance ;
13
11
use serde_json:: json;
14
12
@@ -25,50 +23,50 @@ fn main() {
25
23
"1920x1080" ,
26
24
) ;
27
25
28
- // generate_video_series(
29
- // Duration::from_secs(10),
30
- // Resolution {
31
- // width: 1080,
32
- // height: 1920,
33
- // },
34
- // "1080x1920",
35
- // );
36
-
37
- // generate_video_series(
38
- // Duration::from_secs(10),
39
- // Resolution {
40
- // width: 854,
41
- // height: 480,
42
- // },
43
- // "854x480",
44
- // );
45
-
46
- // generate_video_series(
47
- // Duration::from_secs(10),
48
- // Resolution {
49
- // width: 480,
50
- // height: 854,
51
- // },
52
- // "480x854",
53
- // );
54
-
55
- // generate_video_series(
56
- // Duration::from_secs(10),
57
- // Resolution {
58
- // width: 1440,
59
- // height: 1080,
60
- // },
61
- // "1440x1080",
62
- // );
63
-
64
- // generate_video_series(
65
- // Duration::from_secs(10),
66
- // Resolution {
67
- // width: 1080,
68
- // height: 1440,
69
- // },
70
- // "1080x1440",
71
- // );
26
+ generate_video_series (
27
+ Duration :: from_secs ( 10 ) ,
28
+ Resolution {
29
+ width : 1080 ,
30
+ height : 1920 ,
31
+ } ,
32
+ "1080x1920" ,
33
+ ) ;
34
+
35
+ generate_video_series (
36
+ Duration :: from_secs ( 10 ) ,
37
+ Resolution {
38
+ width : 854 ,
39
+ height : 480 ,
40
+ } ,
41
+ "854x480" ,
42
+ ) ;
43
+
44
+ generate_video_series (
45
+ Duration :: from_secs ( 10 ) ,
46
+ Resolution {
47
+ width : 480 ,
48
+ height : 854 ,
49
+ } ,
50
+ "480x854" ,
51
+ ) ;
52
+
53
+ generate_video_series (
54
+ Duration :: from_secs ( 10 ) ,
55
+ Resolution {
56
+ width : 1440 ,
57
+ height : 1080 ,
58
+ } ,
59
+ "1440x1080" ,
60
+ ) ;
61
+
62
+ generate_video_series (
63
+ Duration :: from_secs ( 10 ) ,
64
+ Resolution {
65
+ width : 1080 ,
66
+ height : 1440 ,
67
+ } ,
68
+ "1080x1440" ,
69
+ ) ;
72
70
}
73
71
74
72
fn generate_video_series ( duration : Duration , resolution : Resolution , name_suffix : & str ) {
@@ -131,7 +129,7 @@ fn generate_video_series(duration: Duration, resolution: Resolution, name_suffix
131
129
fn workingdir ( ) -> PathBuf {
132
130
PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) )
133
131
. join ( "workingdir" )
134
- . join ( "inputs_mp4_maybe " )
132
+ . join ( "inputs_mp4 " )
135
133
}
136
134
137
135
fn generate_video (
@@ -142,26 +140,25 @@ fn generate_video(
142
140
resolution : & Resolution ,
143
141
) -> Result < ( ) > {
144
142
let instance = CompositorInstance :: start ( ) ;
145
- let output_port = instance. get_port ( ) ;
146
143
147
144
instance. send_request (
148
145
"output/output_1/register" ,
149
146
json ! ( {
150
- "type" : "rtp_stream" ,
151
- "transport_protocol" : "tcp_server" ,
152
- "port" : output_port,
153
- // "type": "mp4",
154
- // "path": format!("{}", path.to_string_lossy().to_string()),
147
+ "type" : "mp4" ,
148
+ "path" : format!( "{}" , path. to_string_lossy( ) . to_string( ) ) ,
155
149
"video" : {
156
150
"resolution" : {
157
151
"width" : resolution. width,
158
152
"height" : resolution. height,
159
153
} ,
160
154
"encoder" : {
161
155
"type" : "ffmpeg_h264" ,
162
- "preset" : "ultrafast"
156
+ "preset" : "medium" ,
157
+ "ffmpeg_options" : {
158
+ "crf" : "32"
159
+ }
163
160
} ,
164
- "initial" : scene( text, rgba_color, Duration :: ZERO )
161
+ "initial" : scene( text, rgba_color, resolution , Duration :: ZERO )
165
162
} ,
166
163
} ) ,
167
164
) ?;
@@ -179,29 +176,36 @@ fn generate_video(
179
176
instance. send_request (
180
177
"output/output_1/update" ,
181
178
json ! ( {
182
- "video" : scene( text, rgba_color, pts) ,
179
+ "video" : scene( text, rgba_color, resolution , pts) ,
183
180
"schedule_time_ms" : pts. as_millis( ) ,
184
181
} ) ,
185
182
) ?;
186
183
}
187
184
188
- let gst_thread = thread:: Builder :: new ( ) . name ( "gst sink" . to_string ( ) ) . spawn ( move ||{
189
- let gst_cmd = format ! (
190
- "gst-launch-1.0 -v tcpclientsrc host=127.0.0.1 port={} ! \" application/x-rtp-stream\" ! rtpstreamdepay ! queue ! \" application/x-rtp,media=video,clock-rate=90000,encoding-name=H264,payload=96\" ! queue ! rtph264depay ! h264parse ! mp4mux ! filesink location={}" ,
191
- output_port,
192
- path. to_string_lossy( )
193
- ) ;
194
- Command :: new ( "bash" ) . arg ( "-c" ) . arg ( gst_cmd) . status ( ) . unwrap ( ) ;
195
- } ) . unwrap ( ) ;
196
-
197
185
instance. send_request ( "start" , json ! ( { } ) ) ?;
198
186
199
- gst_thread. join ( ) . unwrap ( ) ;
187
+ thread:: spawn ( || {
188
+ let event_receiver = subscribe ( ) ;
189
+ loop {
190
+ if let Ok ( event) = event_receiver. recv ( ) {
191
+ if event. kind == "OUTPUT_DONE" . to_string ( ) {
192
+ break ;
193
+ }
194
+ }
195
+ }
196
+ } )
197
+ . join ( )
198
+ . unwrap ( ) ;
200
199
201
200
Ok ( ( ) )
202
201
}
203
202
204
- fn scene ( text : & str , rgba_color : & str , pts : Duration ) -> serde_json:: Value {
203
+ fn scene (
204
+ text : & str ,
205
+ rgba_color : & str ,
206
+ resolution : & Resolution ,
207
+ pts : Duration ,
208
+ ) -> serde_json:: Value {
205
209
json ! ( {
206
210
"root" : {
207
211
"type" : "view" ,
@@ -212,24 +216,24 @@ fn scene(text: &str, rgba_color: &str, pts: Duration) -> serde_json::Value {
212
216
{
213
217
"type" : "text" ,
214
218
"text" : text,
215
- "font_size" : 250 ,
216
- "width" : 1920 ,
219
+ "font_size" : resolution . width / 8 ,
220
+ "width" : resolution . width ,
217
221
"align" : "center" ,
218
222
"font_family" : "Comic Sans MS" ,
219
223
} ,
220
224
{ "type" : "view" } ,
221
225
{
222
226
"type" : "view" ,
223
- "bottom" : 100 ,
224
- "right" : 100 ,
225
- "width" : 300 ,
226
- "height" : 100 ,
227
+ "bottom" : resolution . height / 6 ,
228
+ "right" : resolution . width / 8 ,
229
+ "width" : resolution . width / 6 ,
230
+ "height" : resolution . height / 6 ,
227
231
"children" : [
228
232
{
229
233
"type" : "text" ,
230
234
"text" : format!( "{:.2}s" , pts. as_millis( ) as f32 / 1000.0 ) ,
231
- "font_size" : 90 ,
232
- "width" : 300 ,
235
+ "font_size" : resolution . width / 16 ,
236
+ "width" : resolution . width / 6 ,
233
237
"align" : "right" ,
234
238
"font_family" : "Comic Sans MS" ,
235
239
} ,
0 commit comments