aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-split.c
blob: 2014e52248f974f941293d95fc7a9cc1f95c042d (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "stream.h"
#include "util.h"

#include <alloca.h>
#include <string.h>

USAGE("[-L] (file (end-point | 'end')) ...")

int
main(int argc, char *argv[])
{
	struct stream stream;
	size_t *ends, i, parts, n;
	char *to_end;
	FILE *fp;
	int fd, unknown_length = 0;

	ARGBEGIN {
	case 'L':
		unknown_length = 1;
		break;
	default:
		usage();
	} ARGEND;

	if (argc % 2 || !argc)
		usage();

	eopen_stream(&stream, NULL);
	echeck_dimensions(&stream, WIDTH | HEIGHT, NULL);

	parts = (size_t)argc / 2;
	ends = alloca(parts * sizeof(*ends));
	to_end = alloca(parts);

	for (i = 0; i < parts; i++) {
		if ((to_end[i] = !strcmp(argv[i * 2 + 1], "end")))
			ends[i] = unknown_length ? SIZE_MAX : stream.frames;
		else if (tozu(argv[i * 2 + 1], 0, SIZE_MAX, ends + i))
			eprintf("the end point must be an integer in [0, %zu]\n", SIZE_MAX);

		if (i && ends[i] <= ends[i - 1])
			eprintf("the end points must be in strictly ascending order\n");
		if (!unknown_length && ends[i] > stream.frames)
			eprintf("frame %zu is beyond the end of the video\n", ends[i]);
	}

	for (i = 0; i < parts; i++) {
		fd = eopen(argv[i * 2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
		if (!(fp = fdopen(fd, "wb")))
			eprintf("fdopen %s:", argv[i * 2]);

		n = ends[i] - (i ? ends[i - 1] : 0);
		stream.frames = (to_end[i] && unknown_length) ? 0 : n;
		fprint_stream_head(fp, &stream);
		efflush(fp, argv[i * 2]);

		if (esend_frames(&stream, fd, n, argv[i * 2]) != n)
			if (!unknown_length || !to_end[i])
				eprintf("%s: file is shorter than expected\n", stream.file);

		if (fclose(fp))
			eprintf("%s:", argv[i * 2]);
		close(fd);
	}

	return 0;
}