blind

suckless command-line video editing utility
git clone git://git.suckless.org/blind
Log | Files | Refs | README | LICENSE

blind-disperse.c (1378B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "common.h"
      3 
      4 USAGE("(file frames) ...")
      5 
      6 int
      7 main(int argc, char *argv[])
      8 {
      9 	struct stream stream;
     10 	size_t *frames, *framecount, period = 0, parts, i, n;
     11 	int *fds;
     12 
     13 	UNOFLAGS(argc % 2 || !argc);
     14 
     15 	eopen_stream(&stream, NULL);
     16 
     17 	parts      = (size_t)argc / 2;
     18 	frames     = alloca(parts * sizeof(*frames));
     19 	framecount = alloca(parts * sizeof(*framecount));
     20 	fds        = alloca(parts * sizeof(*fds));
     21 
     22 	for (i = 0; i < parts; i++) {
     23 		fds[i] = eopen(argv[i * 2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
     24 		frames[i] = etozu_arg("frames", argv[i * 2 + 1], 1, SIZE_MAX);
     25 	}
     26 	for (i = 0; i < parts; i++) {
     27 		if (frames[i] > SIZE_MAX - period)
     28 			eprintf("the sum of selected frame intervals exceeds %zu\n", SIZE_MAX);
     29 		period += frames[i];
     30 	}
     31 	for (n = stream.frames / period, i = 0; i < parts; i++)
     32 		framecount[i] = n * frames[i];
     33 	for (n = stream.frames % period, i = 0; i < parts; i++) {
     34 		framecount[i] += MIN(n, frames[i]);
     35 		n -= MIN(n, frames[i]);
     36 	}
     37 
     38 	for (i = 0; i < parts; i++)
     39 		if (DPRINTF_HEAD(fds[i], framecount[i], stream.width, stream.height, stream.pixfmt) < 0)
     40 			eprintf("dprintf %s:", argv[i * 2]);
     41 	for (i = 0; i < parts; i++, i = i == parts ? 0 : i)
     42 		if (esend_frames(&stream, fds[i], frames[i], argv[i * 2]) != frames[i])
     43 			break;
     44 	for (i = 0; i < parts; i++)
     45 		close(fds[i]);
     46 
     47 	return 0;
     48 }