瀏覽代碼

对 ts 片 seek 单独处理

DevYK 3 年之前
父節點
當前提交
5d7927a553
共有 2 個文件被更改,包括 44 次插入24 次删除
  1. 27 7
      src/ffmpeg/av_decode.cpp
  2. 17 17
      src/video_similarity_comparison.cpp

+ 27 - 7
src/ffmpeg/av_decode.cpp

@@ -91,11 +91,11 @@ static int av_decode(struct DecoderContext *pContext, AVPacket *pPacket, enum AV
                                     (av_q2d(pContext->avformatContext->streams[pContext->st_index[type]]->time_base)));
 
 
-        int64_t offset = 0;
-        if (pContext->offset != AV_NOPTS_VALUE) {
-            offset += pContext->offset / 1000;
-            pts -= offset;
-        }
+//        int64_t offset = 0;
+//        if (pContext->offset != AV_NOPTS_VALUE) {
+//            offset += pContext->offset / 1000;
+//            pts -= offset;
+//        }
 
         if (pContext->end_time > 0 && pts > pContext->end_time && type == AVMEDIA_TYPE_VIDEO) {
             av_frame_free(&out_frame);
@@ -277,14 +277,34 @@ long initDecoder(const char *url, int force_Iframe, DisableMediaType disableMedi
     return ret == 0 ? (long) dctx : ret;
 }
 
+static int is_realtime(AVFormatContext *s) {
+    if (!strcmp(s->iformat->name, "rtp")
+        || !strcmp(s->iformat->name, "rtsp")
+        || !strcmp(s->iformat->name, "sdp")
+        || !strcmp(s->iformat->name, "hls")
+            )
+        return 1;
+
+    if (s->pb && (!strncmp(s->url, "rtp:", 4)
+                  || !strncmp(s->url, "udp:", 4)
+    )
+            )
+        return 1;
+    return 0;
+}
+
 void seekToMs(DecoderContext *dctx, int64_t offset) {
     int64_t timestamp = dctx->start_time * 1000;
     if (offset != AV_NOPTS_VALUE)
         timestamp += offset;
     if (timestamp > 0) {
         avformat_flush(dctx->avformatContext);
-        int ret = avformat_seek_file(dctx->avformatContext, -1, INT64_MIN, timestamp,
-                                     timestamp,
+
+        int re = is_realtime(dctx->avformatContext);
+        int64_t tartget_pos = re ? MAX(0, timestamp - 10000000) : timestamp;
+
+        int ret = avformat_seek_file(dctx->avformatContext, -1, INT64_MIN, tartget_pos,
+                                     tartget_pos,
                                      AVSEEK_FLAG_BACKWARD);
         if (ret >= 0) {
             if (dctx->videoCodecContext)

+ 17 - 17
src/video_similarity_comparison.cpp

@@ -58,39 +58,39 @@ static int *list_sim_compare(std::vector<ImageHashModel *> a, std::vector<ImageH
 static void run(){
 
     const char *input_json = "{\n"
-                             "  \"videoPath\": \"http://rescdn.yishihui.com/longvideo/transcode/crawler/video/prod/20210708/VIDEO_XIGUA_6981987670008267267.m3u8\",\n"
+                             "  \"videoPath\": \"http://rescdn.yishihui.com/longvideo/transcode/video/qa/20220314/6267824Z0EoT5Pr61RFNcKI07.mp4\",\n"
                              "  \"clips\": [\n"
 //                             "    {\n"
-//                             "      \"startTimeMs\": 119900,\n"
-//                             "      \"endTimeMs\": 120900\n"
+//                             "      \"startTimeMs\": 87200,\n"
+//                             "      \"endTimeMs\": 88200\n"
 //                             "    },\n"
 //                             "    {\n"
-//                             "      \"startTimeMs\": 239800,\n"
-//                             "      \"endTimeMs\": 240800\n"
+//                             "      \"startTimeMs\": 174400,\n"
+//                             "      \"endTimeMs\": 175400\n"
 //                             "    },\n"
 //                             "    {\n"
-//                             "      \"startTimeMs\": 359700,\n"
-//                             "      \"endTimeMs\": 360700\n"
+//                             "      \"startTimeMs\": 261600,\n"
+//                             "      \"endTimeMs\": 262600\n"
 //                             "    },\n"
 //                             "    {\n"
-//                             "      \"startTimeMs\": 479600,\n"
-//                             "      \"endTimeMs\": 480600\n"
+//                             "      \"startTimeMs\": 348800,\n"
+//                             "      \"endTimeMs\": 349800\n"
 //                             "    },\n"
 //                             "    {\n"
-//                             "      \"startTimeMs\": 599500,\n"
-//                             "      \"endTimeMs\": 600500\n"
+//                             "      \"startTimeMs\": 436000,\n"
+//                             "      \"endTimeMs\": 437000\n"
 //                             "    },\n"
 //                             "    {\n"
-//                             "      \"startTimeMs\": 719400,\n"
-//                             "      \"endTimeMs\": 720400\n"
+//                             "      \"startTimeMs\": 523200,\n"
+//                             "      \"endTimeMs\": 524200\n"
 //                             "    },\n"
 //                             "    {\n"
-//                             "      \"startTimeMs\": 87200,\n"
-//                             "      \"endTimeMs\": 88200\n"
+//                             "      \"startTimeMs\": 610400,\n"
+//                             "      \"endTimeMs\": 611400\n"
 //                             "    },\n"
                              "    {\n"
-                             "      \"startTimeMs\": 87200,\n"
-                             "      \"endTimeMs\": 88200\n"
+                             "      \"startTimeMs\": 697600,\n"
+                             "      \"endTimeMs\": 698600\n"
                              "    }\n"
                              "  ]\n"
                              "}";