From 31660ca5df7e1a906defdab36823aa8791025a61 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 14 Jan 2017 09:08:56 +0100 Subject: blind-from-video and blind-split: add -L This, combined with blind-to-video, allows the user to split a video file by frames (rather than time as with ffmpeg) into multiple files without ever storing the video in a raw format. This is feature is important because ever short video can take a 100 GB in raw format. --- src/blind-from-video.c | 42 ++++++++++++++++++++++++++++++------------ src/blind-split.c | 36 +++++++++++++++++++++++++++--------- 2 files changed, 57 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/blind-from-video.c b/src/blind-from-video.c index 6dafeef..178ab17 100644 --- a/src/blind-from-video.c +++ b/src/blind-from-video.c @@ -14,7 +14,7 @@ #include #include -USAGE("[-r frame-rate] [-w width -h height] [-d] input-file output-file") +USAGE("[-r frame-rate] [-w width -h height] [-dL] input-file output-file") static int draft = 0; @@ -94,7 +94,7 @@ get_metadata(char *file, size_t *width, size_t *height) } static void -convert_segment(char *buf, size_t n, int fd, char *file) +convert_segment(char *buf, size_t n, int fd, const char *file) { typedef double pixel_t[4]; size_t i, ptr; @@ -135,7 +135,7 @@ convert_segment(char *buf, size_t n, int fd, char *file) } static void -convert(char *infile, int outfd, char *outfile, size_t width, size_t height, char *frame_rate) +convert(const char *infile, int outfd, const char *outfile, size_t width, size_t height, const char *frame_rate) { char geometry[2 * 3 * sizeof(size_t) + 2], buf[BUFSIZ]; const char *cmd[13]; @@ -206,17 +206,20 @@ main(int argc, char *argv[]) char head[STREAM_HEAD_MAX]; char *frame_rate = NULL; char *infile; - char *outfile; + const char *outfile; char *data; ssize_t headlen; size_t length, frame_size; - int outfd; + int outfd, skip_length = 0; struct stat st; ARGBEGIN { case 'd': draft = 1; break; + case 'L': + skip_length = 1; + break; case 'r': frame_rate = EARG(); break; @@ -245,7 +248,20 @@ main(int argc, char *argv[]) eprintf("video frame too large\n"); frame_size *= 4 * sizeof(double); - outfd = eopen(outfile, O_RDWR | O_CREAT | O_TRUNC, 0666); + if (!strcmp(outfile, "-")) { + outfile = ""; + outfd = STDOUT_FILENO; + if (!skip_length) + eprintf("standard out as output file is only allowed with -L\n"); + } else { + outfd = eopen(outfile, O_RDWR | O_CREAT | O_TRUNC, 0666); + } + + if (skip_length) { + sprintf(head, "%zu %zu %zu %s\n%cuivf%zn", frames, width, height, "xyza", 0, &headlen); + ewriteall(outfd, head, (size_t)headlen, outfile); + } + convert(infile, outfd, outfile, width, height, frame_rate); if (fstat(outfd, &st)) @@ -256,12 +272,14 @@ main(int argc, char *argv[]) eprintf(": incomplete frame"); frames = length / frame_size; - sprintf(head, "%zu %zu %zu %s\n%cuivf%zn", frames, width, height, "xyza", 0, &headlen); - ewriteall(outfd, head, (size_t)headlen, outfile); - data = mmap(0, length + (size_t)headlen, PROT_READ | PROT_WRITE, MAP_SHARED, outfd, 0); - memmove(data + headlen, data, length); - memcpy(data, head, (size_t)headlen); - munmap(data, length + (size_t)headlen); + if (!skip_length) { + sprintf(head, "%zu %zu %zu %s\n%cuivf%zn", frames, width, height, "xyza", 0, &headlen); + ewriteall(outfd, head, (size_t)headlen, outfile); + data = mmap(0, length + (size_t)headlen, PROT_READ | PROT_WRITE, MAP_SHARED, outfd, 0); + memmove(data + headlen, data, length); + memcpy(data, head, (size_t)headlen); + munmap(data, length + (size_t)headlen); + } close(outfd); return 0; diff --git a/src/blind-split.c b/src/blind-split.c index 6b06da5..038b3b6 100644 --- a/src/blind-split.c +++ b/src/blind-split.c @@ -9,17 +9,27 @@ #include #include -USAGE("(file (end-point | 'end')) ...") +USAGE("[-L] (file (end-point | 'end')) ...") int main(int argc, char *argv[]) { struct stream stream; size_t *ends, i, parts, ptr, end, frame_size, n; + char *to_end; FILE *fp; - int fd; + int fd, unknown_length = 0; - ENOFLAGS(argc < 2 || argc % 2); + ARGBEGIN { + case 'L': + unknown_length = 1; + break; + default: + usage(); + } ARGEND; + + if (argc < 2 || argc % 2) + usage(); stream.file = ""; stream.fd = STDIN_FILENO; @@ -31,15 +41,19 @@ main(int argc, char *argv[]) parts = (size_t)argc / 2; ends = alloca(parts * sizeof(*ends)); + to_end = alloca(parts); for (i = 0; i < parts; i++) { - if (!strcmp(argv[i * 2 + 1], "end")) - ends[i] = stream.frames; - else if (tozu(argv[i * 2 + 1], 0, SIZE_MAX, ends + i)) + to_end[i] = 0; + if (!strcmp(argv[i * 2 + 1], "end")) { + ends[i] = unknown_length ? SIZE_MAX : stream.frames; + to_end[i] = 1; + } 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 (ends[i] > stream.frames) + if (!unknown_length && ends[i] > stream.frames) eprintf("frame %zu is beyond the end of the video\n", ends[i]); } @@ -54,7 +68,7 @@ main(int argc, char *argv[]) fprint_stream_head(fp, &stream); efflush(fp, argv[i * 2]); - for (end = ends[i] * frame_size; ptr < end; ptr += n) { + for (end = to_end[i] ? SIZE_MAX : ends[i] * frame_size; ptr < end; ptr += n) { n = end - ptr; if (stream.ptr) { n = stream.ptr < n ? stream.ptr : n; @@ -63,8 +77,12 @@ main(int argc, char *argv[]) } else if ((n = eread_stream(&stream, n))) { ewriteall(fd, stream.buf, n, argv[i * 2]); stream.ptr = 0; - } else { + } else if (ptr % frame_size) { + eprintf("%s: incomplete frame\n", stream.file); + } else if (!unknown_length) { eprintf("%s: file is shorter than expected\n", stream.file); + } else { + break; } } -- cgit v1.2.3-70-g09d2