summaryrefslogtreecommitdiff
path: root/src/findcut.c
blob: 2b2bc71b8123d35a63a09b3635aa405c21f0e4b8 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

#include "video_decode.h"
#include "util.h"

static void
usage(void)
{
	fprintf(stderr, "findcut [-dh] [-t THRESHOLD] VIDEO CUT\n"
			"\n"
			"OPTIONS\n"
			"\t-d\tprint diff for every frame\n"
			"\t-h\tprint this help message\n"
			"\t-t\tthreshold matching\n"
			"\t-T\tprint timestamp of every frame\n");

	exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
	struct video_decode *video_vd;
	struct video_decode *cut_vd;
	struct video_frame *cur_frame = NULL, *cut_frame = NULL;
	int ret, ch;
	int diff_flag = 0, time_flag = 0;
	int64_t timestamp = 0;
	int64_t threshold = 10;
	double diff = 0.0;

	while ((ch = getopt(argc, argv, "dtTh")) != -1) {
		switch (ch) {
		case 'd':
			diff_flag = 1;	/* print diff of every frame */
			break;
		case 't':
			threshold = strtol(optarg, NULL, 10);
			break;
		case 'T':
			time_flag = 1;
			break;
		case 'h':
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 2)
		usage();

	ret = video_decode_init(&cut_vd, argv[1], timestamp);
	if (ret < 0)
		return -ret;

	/* just get first frame of the cut */
	video_decode_get_frame(cut_vd, &cut_frame);

	ret = video_decode_init(&video_vd, argv[0], timestamp);
	if (ret < 0)
		return -ret;

	for (int i = 0; video_decode_get_frame(video_vd, &cur_frame) > 0; ++i) {
		diff = frame_diff(cut_frame, cur_frame);

		if (diff <= threshold || diff_flag) {
			if (time_flag)
				printf("%lld", cur_frame->dts);
			else
				print_time(cur_frame->dts);

			printf(" %f\n", diff);
		}
	}

	video_decode_free_frame(&cur_frame);
	video_decode_free_frame(&cut_frame);
	video_decode_uninit(&video_vd);
	video_decode_uninit(&cut_vd);
	return EXIT_SUCCESS;
}