summaryrefslogtreecommitdiff
path: root/src/findcut.c
blob: 816b27b0255d3b5ec6b41e69107c67778a8ffdfc (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.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, range_flag = 0, last_flag = 0;
	int64_t timestamp = 0;
	int64_t threshold = 10;
	double diff = 0.0;

	while ((ch = getopt(argc, argv, "dtThrl")) != -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 'r':
			range_flag = 1;
			break;
		case 'l':
			last_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;

	int64_t last_time=0;
	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) {
			int64_t time = last_flag ? last_time : cur_frame->dts;
			if (time_flag)
				printf("%" PRId64, time);
			else
				print_time(time);

			if (range_flag) {
				printf(" ");
				if (time_flag)
					printf("%" PRId64, cur_frame->dts+cut_vd->duration);
				else
					print_time(cur_frame->dts+cut_vd->duration);

				printf("\n");
			} else
				printf(" %f\n", diff);
			fflush(stdout);
		}
		last_time=cur_frame->dts;
	}

	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;
}