commit 31660ca5df7e1a906defdab36823aa8791025a61
parent 4a0bec23f06a4e119531fb8a62c07d346116c709
Author: Mattias Andrée <maandree@kth.se>
Date: Sat, 14 Jan 2017 09:08:56 +0100
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.
Diffstat:
3 files changed, 74 insertions(+), 35 deletions(-)
diff --git a/examples/split/Makefile b/examples/split/Makefile
@@ -4,15 +4,21 @@ SHELL = bash
# We need Bash's process substitution operator >()
# because we want to convert the files back to a
# cooked format, because raw takes a serious amount
-# of space.
+# of space. It is of course also possible to use
+# FIFO:s.
DRAFT = -d
# Useful for better performance when not working
# with colours or not caring about colours.
FFMPEG_ARGS = -c:v libx264 -preset veryslow -crf 0 -pix_fmt yuv444p
-
-TEMPFILE = temp.uivf
+# ↑~~~~~~~~~~~ ↑~~~~~~~~~~~~~~~ ↑~~~~~~~~~~~~~~~~~~~~~~
+# │ │ │
+# │ │ └──── Lossless
+# │ │
+# │ └──── High compression
+# │
+# └──── H.264, a lossless-capable codec
FRAME_1 = 10
FRAME_2 = 20
@@ -20,20 +26,17 @@ FRAME_3 = 30
FRAME_4 = 40
FRAME_5 = end
-1.mkv 2.mkv 3.mkv 4.mkv 5.mkv: $(TEMPFILE)
+1.mkv 2.mkv 3.mkv 4.mkv 5.mkv: $(INPUT_VIDEO)
framerate=$$(ffprobe -v quiet -show_streams -select_streams v - < "$(INPUT_VIDEO)" | \
grep '^r_frame_rate=' | cut -d = -f 2) && \
- ../../blind-split >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 1.mkv) $(FRAME_1) \
- >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 2.mkv) $(FRAME_2) \
- >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 3.mkv) $(FRAME_3) \
- >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 4.mkv) $(FRAME_4) \
- >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 5.mkv) $(FRAME_5) \
- < $(TEMPFILE)
-
-$(TEMPFILE): $(INPUT_VIDEO)
- ../../blind-from-video $(DRAFT) $(INPUT_VIDEO) $(TEMPFILE)
+ ../../blind-from-video -L $(DRAFT) "$(INPUT_VIDEO)" - | \
+ ../../blind-split -L >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 1.mkv) $(FRAME_1) \
+ >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 2.mkv) $(FRAME_2) \
+ >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 3.mkv) $(FRAME_3) \
+ >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 4.mkv) $(FRAME_4) \
+ >(../../blind-to-video $(DRAFT) $${framerate} $(FFMPEG_ARGS) 5.mkv) $(FRAME_5)
clean:
- -rm 1.mkv 2.mkv 3.mkv 4.mkv 5.mkv $(TEMPFILE)
+ -rm 1.mkv 2.mkv 3.mkv 4.mkv 5.mkv
.PHONY: clean
diff --git a/src/blind-from-video.c b/src/blind-from-video.c
@@ -14,7 +14,7 @@
#include <string.h>
#include <unistd.h>
-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 = "<stdout>";
+ 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("<subprocess>: 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
@@ -9,17 +9,27 @@
#include <string.h>
#include <unistd.h>
-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 = "<stdin>";
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;
}
}