video_similarity_comparison.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // Created by 阳坤 on 2022/3/1.
  3. //
  4. #include "utils/video_similarity.h"
  5. #include "utils/count_down_latch.h"
  6. #include "ffmpeg/av_tools.h"
  7. extern "C" {
  8. #include "utils/md5.h"
  9. }
  10. typedef struct VsimBean {
  11. const char *filepath;
  12. const char *image_md5;
  13. std::vector<VideoSimilarityModel *> lists;
  14. pthread_t id;
  15. } VsimBean;
  16. static void run(const char *filepath, std::vector<VideoSimilarityModel *> *list) {
  17. long id = video_similarity_detection_init(filepath, nullptr);
  18. if (id > 0) {
  19. int ret = video_similarity_detection_start(id, 0, AUDIO,
  20. list);
  21. // char md5_str[64] = {0};
  22. // if (get_file_md5(vsimBean->filepath, md5_str) > 0);
  23. // vsimBean->image_md5 = strdup(md5_str);
  24. video_similarity_detection_close(id);
  25. }
  26. }
  27. static void *run_thread(void *p) {
  28. auto *vsimBean = (VsimBean *) p;
  29. run(vsimBean->filepath, &vsimBean->lists);
  30. return 0;
  31. }
  32. static int *list_sim_compare(std::vector<ImageHashModel *> a, std::vector<ImageHashModel *> b) {
  33. int minFrames = MIN(a.size(), b.size());
  34. int maxFrames = MAX(a.size(), b.size());
  35. int sim_frame_count = 0;
  36. int arr[2];
  37. //比较帧指纹
  38. for (int k = 0; k < minFrames; ++k) {
  39. float v_sim = fingerprint_compare(a[k]->image_hash, b[k]->image_hash,
  40. minFrames);
  41. if (v_sim > 0.90) {
  42. sim_frame_count++;
  43. }
  44. }
  45. arr[0] = sim_frame_count;
  46. arr[1] = maxFrames;
  47. return arr;
  48. }
  49. typedef struct Test {
  50. const char *input;
  51. CountDownLatch *countDownLatch;
  52. };
  53. static void *run(void *p) {
  54. auto *test = (Test *) p;
  55. const char *ret_json = get_video_similarity_list(test->input);
  56. printf("ret_json=%s\n", ret_json);
  57. test->countDownLatch->countDown();
  58. free(test);
  59. pthread_exit(NULL);
  60. }
  61. /**
  62. * 1、判断比对文件的 MD5 是否相等
  63. * 1.1、如果相等,
  64. * 1.1.1、判断媒体数据 mate 信息是否一致,如果一致,判断前面 N 关键帧的指纹码
  65. * 1.2、如果不等
  66. * 1.2.1、判断媒体数据 mate 信息是否一致, 如果一致,判断前面 N 关键帧的指纹码
  67. * @param argc
  68. * @param argv
  69. * @return
  70. */
  71. int main(int argc, char *argv[]) {
  72. int threads = 1;
  73. const char *inputs[1];
  74. // inputs[0] = "{\"videoPath\":\"http://rescdn.yishihui.com/longvideo/transcode/video/b46fd76f98364b3abad8c1297a868f82-1526976612340.m3u8\",\"clips\":[{\"startTimeMs\":3000,\"endTimeMs\":4000},{\"startTimeMs\":6000,\"endTimeMs\":7000},{\"startTimeMs\":9000,\"endTimeMs\":10000},{\"startTimeMs\":12000,\"endTimeMs\":13000},{\"startTimeMs\":15000,\"endTimeMs\":16000},{\"startTimeMs\":18000,\"endTimeMs\":19000},{\"startTimeMs\":21000,\"endTimeMs\":22000},{\"startTimeMs\":24000,\"endTimeMs\":25000}]}";
  75. inputs[0] = "{\"videoPath\":\"http://rescdn.yishihui.com/longvideo/transcode/video/e6b5cfa854b24e3f87d133e3b2da0ade1530635451140-randomKgqMSnitde.mp4\",\"clips\":[{\"startTimeMs\":14000,\"endTimeMs\":15000},{\"startTimeMs\":28000,\"endTimeMs\":29000},{\"startTimeMs\":42000,\"endTimeMs\":43000},{\"startTimeMs\":56000,\"endTimeMs\":57000},{\"startTimeMs\":70000,\"endTimeMs\":71000},{\"startTimeMs\":84000,\"endTimeMs\":85000},{\"startTimeMs\":98000,\"endTimeMs\":99000},{\"startTimeMs\":112000,\"endTimeMs\":113000}]}";
  76. // inputs[2] = "{\"videoPath\":\"http://rescdn.yishihui.com/longvideo/transcode/video/3dbf36e8ec27479581514b8b7b4c8034-1526924772832-safe1594892815.m3u8\",\"clips\":[{\"startTimeMs\":6000,\"endTimeMs\":7000},{\"startTimeMs\":12000,\"endTimeMs\":13000},{\"startTimeMs\":18000,\"endTimeMs\":19000},{\"startTimeMs\":24000,\"endTimeMs\":25000},{\"startTimeMs\":30000,\"endTimeMs\":31000},{\"startTimeMs\":36000,\"endTimeMs\":37000},{\"startTimeMs\":42000,\"endTimeMs\":43000},{\"startTimeMs\":48000,\"endTimeMs\":49000}]}";
  77. // inputs[3] = "{\"videoPath\":\"http://rescdn.yishihui.com/longvideo/transcode/video/3dbf36e8ec27479581514b8b7b4c8034-1526924772832-safe1594892815.m3u8\",\"clips\":[{\"startTimeMs\":6000,\"endTimeMs\":7000},{\"startTimeMs\":12000,\"endTimeMs\":13000},{\"startTimeMs\":18000,\"endTimeMs\":19000},{\"startTimeMs\":24000,\"endTimeMs\":25000},{\"startTimeMs\":30000,\"endTimeMs\":31000},{\"startTimeMs\":36000,\"endTimeMs\":37000},{\"startTimeMs\":42000,\"endTimeMs\":43000},{\"startTimeMs\":48000,\"endTimeMs\":49000}]}";
  78. // for (;;) {
  79. CountDownLatch *countDownLatch = new CountDownLatch(threads);
  80. for (int i = 0; i < threads; ++i) {
  81. Test *test = static_cast<Test *>(malloc(sizeof(Test)));
  82. test->countDownLatch = countDownLatch;
  83. test->input = inputs[i];
  84. pthread_t id;
  85. pthread_create(&id,0,run,test);
  86. }
  87. countDownLatch->await();
  88. delete countDownLatch;
  89. // }
  90. return 1;
  91. // const char *out_json = "{\n"
  92. // " \"videoPath\": \"\\\"/root/data/xxx.mp4\\\"\",\n"
  93. // " \"clips\": [\n"
  94. // " {\n"
  95. // " \"startTimeMs\": 15000,\n"
  96. // " \"endTimeMs\": 25000,\n"
  97. // " \"fingerprintCode\": [\n"
  98. // " \"\\\"0001000100101010101010\\\"\",\n"
  99. // " \"\\\"0001000100101010101010\\\"\",\n"
  100. // " \"\\\"0001000100101010101010\\\"\",\n"
  101. // " \"\\\"0001000100101010101010\\\"\"\n"
  102. // " ]\n"
  103. // " }\n"
  104. // " ]\n"
  105. // "}";
  106. // auto vs = json2VideoSimilarity(input_json);
  107. // auto json = videoSimilarity2json(vs);
  108. int32_t size = 7;
  109. const char *filepath[size];
  110. //票圈视频iOS客户端上传
  111. filepath[0] = "http://rescdn.yishihui.com/longvideo/video/vpc/20220309/17025689Igao51Q0IMBYw8aNKH";
  112. //ios 原生上传
  113. filepath[1] = "http://rescdn.yishihui.com/longvideo/video/vpc/20220309/17025689VvI5KURrrxzlZ0Kcdv";
  114. //iOS h5上传
  115. filepath[2] = "http://rescdn.yishihui.com/longvideo/video/vpc/20220309/17025689iBy9WFswYdBNblY9au";
  116. //票圈视频安卓客户端上传
  117. filepath[3] = "http://rescdn.yishihui.com/longvideo/video/vpc/20220309/17677590exDXG6q75ELejK7aMY";
  118. //安卓h5上传
  119. filepath[4] = "http://rescdn.yishihui.com/longvideo/video/vpc/20220309/176775906GoWtUCYoVZYDbO1XX";
  120. //安卓原生上传
  121. filepath[5] = "";
  122. //PC 上传
  123. // filepath[5] = "http://rescdn.yishihui.com/longvideo/video/vpc/20220309/17025689nSyGzifJFr4Apl0zc8";
  124. filepath[5] = "http://rescdn.yishihui.com/longvideo/video/vpc/20220310/701939000F0qpyCLetflI3COz";
  125. http://rescdn.yishihui.com/longvideo/video/vpc/20220304/17938576TxEh0UJXpzcoYRJuHu
  126. //本地 local 上传的视频
  127. filepath[6] = "/Users/devyk/Data/Project/sample/github_code/OpenCVSample/temp/19581045gIiFKepxbbplF3XtsG.mp4";
  128. // run(vs->videoPath,&vs->clips);
  129. // const char * ret = videoSimilarity2json(vs);
  130. // const char *local_url = "/Users/devyk/Data/Project/sample/github_code/OpenCVSample/temp/19581045gIiFKepxbbplF3XtsG.mp4";
  131. // std::vector<VideoSimilarityModel *> local_list;
  132. // run(local_url, &local_list);
  133. std::vector<VideoSimilarityModel *> lists[size];
  134. int split_count = 10;
  135. // int total = 3960000;
  136. int ii = 1000 * 60 * 6;
  137. int start = ii;
  138. for (int i = 0; i < size; ++i) {
  139. auto start_time = getCurrentTimeMills();
  140. start = ii;
  141. for (int j = 0; j < split_count; ++j) {
  142. if (j == 0 || j == split_count - 1) {
  143. continue;
  144. }
  145. auto *item = new VideoSimilarityModel();
  146. item->startTimeMs = start;
  147. item->endTimeMs = item->startTimeMs + 1000;
  148. lists[i].push_back(item);
  149. start += ii;
  150. }
  151. LOGE(">>>filepath=%s \n", filepath[i]);
  152. run(filepath[i], &lists[i]);
  153. LOGE("<<<filepath=%s cost time=%lld \n", filepath[i], getCurrentTimeMills() - start_time);
  154. }
  155. //本地 local
  156. std::vector<VideoSimilarityModel *> local = lists[size - 1];
  157. //相似 hash 值
  158. int test_vis[6][8] = {0};
  159. //所有的 帧
  160. int test_vis_total[6][8] = {0};
  161. for (int j = 0; j < size - 1; ++j) {
  162. for (int g = 0; g < lists[j].size(); ++g) {
  163. int *cur_sim = list_sim_compare(local[g]->hashs, lists[j][g]->hashs);
  164. int a = cur_sim[0];
  165. int b = cur_sim[1];
  166. test_vis[j][g] = a;
  167. test_vis_total[j][g] = b;
  168. }
  169. }
  170. for (int i = 0; i < size - 1; ++i) {
  171. int a_t = 0, b_t = 0;
  172. for (int j = 0; j < 8; ++j) {
  173. a_t += test_vis[i][j];
  174. b_t += test_vis_total[i][j];
  175. }
  176. LOGE("%s %s 相似度 = %f \n", filepath[6], filepath[i], a_t * 1.0 / b_t * 1.0);
  177. }
  178. printf("");
  179. LOGE("end \n");
  180. return 0;
  181. }