blind

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

blind-split.c (1622B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "common.h"
      3 
      4 USAGE("[-L] (file (end-point | 'end')) ...")
      5 
      6 int
      7 main(int argc, char *argv[])
      8 {
      9 	struct stream stream;
     10 	size_t *ends, i, parts, n;
     11 	char *to_end;
     12 	FILE *fp;
     13 	int fd, unknown_length = 0;
     14 
     15 	ARGBEGIN {
     16 	case 'L':
     17 		unknown_length = 1;
     18 		break;
     19 	default:
     20 		usage();
     21 	} ARGEND;
     22 
     23 	if (argc % 2 || !argc)
     24 		usage();
     25 
     26 	eopen_stream(&stream, NULL);
     27 	echeck_dimensions(&stream, WIDTH | HEIGHT, NULL);
     28 
     29 	parts = (size_t)argc / 2;
     30 	ends = alloca(parts * sizeof(*ends));
     31 	to_end = alloca(parts);
     32 
     33 	for (i = 0; i < parts; i++) {
     34 		if ((to_end[i] = !strcmp(argv[i * 2 + 1], "end")))
     35 			ends[i] = unknown_length ? SIZE_MAX : stream.frames;
     36 		else if (tozu(argv[i * 2 + 1], 0, SIZE_MAX, ends + i))
     37 			eprintf("the end point must be an integer in [0, %zu]\n", SIZE_MAX);
     38 
     39 		if (i && ends[i] <= ends[i - 1])
     40 			eprintf("the end points must be in strictly ascending order\n");
     41 		if (!unknown_length && ends[i] > stream.frames)
     42 			eprintf("frame %zu is beyond the end of the video\n", ends[i]);
     43 	}
     44 
     45 	for (i = 0; i < parts; i++) {
     46 		fd = eopen(argv[i * 2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
     47 		if (!(fp = fdopen(fd, "wb")))
     48 			eprintf("fdopen %s:", argv[i * 2]);
     49 
     50 		n = ends[i] - (i ? ends[i - 1] : 0);
     51 		stream.frames = (to_end[i] && unknown_length) ? 0 : n;
     52 		fprint_stream_head(fp, &stream);
     53 		efflush(fp, argv[i * 2]);
     54 
     55 		if (esend_frames(&stream, fd, n, argv[i * 2]) != n)
     56 			if (!unknown_length || !to_end[i])
     57 				eprintf("%s: file is shorter than expected\n", stream.file);
     58 
     59 		if (fclose(fp))
     60 			eprintf("%s:", argv[i * 2]);
     61 		close(fd);
     62 	}
     63 
     64 	return 0;
     65 }