🎄

Agentic に動画検索してみた

に公開

はじめに

CyberAgent AI Lab の鈴木です。
去年とは趣向を変えて今年は動画検索を試してみます。動画検索といっても色々な設定があるのですが、今回は「動画データ群からクエリにマッチするモーメント(時間区間)を探す」という設定を扱います。これは学術的には Video Corpus Moment Retrieval (VCMR)[1] と呼ばれるらしいです。

最近の研究を調べると、特徴ベクトルで密に類似度を測る手法[2][3][4]がメジャーのようですが、サクッとやるには重そう...ということで今回は動画をテキストドキュメントのように扱い、既存の効率的なテキスト検索の枠組みに載せて VCMR を試します。また、複雑なクエリや曖昧なクエリに対応できるよう、LLM に検索システムを操作させる Agentic Retrieval 的[5]なアプローチを試します。ざっくり、動画をテキスト化し、ElasticSearch でインデクシング、LLM にそれを用いて賢く検索させる、という流れです。


Gradio で作成したデモ画面

動画をテキスト化

まずは動画をテキスト化します。今回は広告系の動画なども含む公開データセット AdsQA[6] を使用します。

広告動画は複数シーンを繋げて構成されることが多いので、PySceneDetect を用いてシーンに分割します。PySceneDetect は動画内のシーン変化点を検出するライブラリです。AdsQA の動画は全部で 1,742 本あり、シーン分割することで約 38,000 シーンに分割されました。

次に各シーンの説明文を用意します。今回は ollama を使用して VLM で説明文を生成します。ollama はローカルで LLM/VLM を手軽に動かせるツールで、導入も簡単でした。今回は Qwen-VL-3B という最軽量レベルの VLM を使用し、各シーンから間引いたフレームを入力して、シーンの説明文を生成しました。

シーンと説明文は以下のようなものになります。


A person wearing headphones is sitting in front of a microphone and a computer. The person is typing on a keyboard.


The video clip shows a series of frames with blue balloons and text promoting a fitness offer. The text encourages viewers to join online or in a club today, with a special offer ending on January 31, 2023.

インデクシング

次にシーンを検索可能にするために、生成したシーン説明文を ElasticSearch でインデクシングします。ElasticSearch は高速なテキスト検索エンジンです。

今回は各シーンを一つのドキュメントとして扱い、スコアリングはデフォルトの BM25 を使用しました。BM25 はクエリ内の単語がドキュメント内に出現する頻度や、ドキュメント全体での単語の希少性を考慮して関連度スコアを計算します。各単語が含まれるドキュメントを事前にまとめておく転置インデックスにより高速に検索できるというのが、シンプルですが面白い仕組みです。BM25 についてはこちらの記事が参考になります。

検索ツールを持たせた LLM エージェントの構築

シーン検索のシステムはできましたが、今回やりたいのは複数シーンで構成される可能性のある"モーメント"の検索であり、このままではそれはできません。また、テキストの一致を元に検索するため、表現の違いによる検索漏れが起きる可能性があります。

そこで、作った検索エンジンをツールとして LLM エージェントに渡し、それを使用して試行錯誤しながら渡したクエリに対応するモーメントを見つけてもらうようにします。具体的には ReAct [7] と呼ばれる LLM が自律的に必要なツールを選択実行しながらタスクを解決するフレームワークを用います。LLM の生成するクエリは元のクエリの言い換えや具体化した表現を含むことができるため、表現の違いによる検索漏れを軽減できます。また、LLM は複数のクエリで検索し、結果を統合することで複数シーンにまたがるモーメントも見つけることができます。

実装したツールは、複数クエリを受け取って ElasticSearch に問い合わせを行い、同じ動画内の連続シーンを一つのモーメントとしてまとめ、そのモーメントの説明文を返す、というものです。実装には LangChain を使用しました。

from elasticsearch import Elasticsearch
from langchain.tools import tool

@tool
def search_moments(queries: list[dict], top_k: int = 5) -> list[Moment]:
    """長いので省略しますが LLM のためにツールの使い方を Docstring に書いておきます。"""
    all_clips = {}
    for query_dsl in queries:
        hits = es.search(index="clips", query=query_dsl)
        for hit in hits:
            all_clips[hit.file] = hit
    moments = group_consecutive_clips(all_clips)
    return sorted(moments, key=lambda m: m.score, reverse=True)[:top_k]

group_consecutive_clips は同じ動画の連続シーンをグループ化します(例: clip-001, clip-002, clip-004 がヒット → clip-001〜002 と clip-004 の 2 モーメント)。
モーメントのスコアは、各クエリについてのモーメント内の最高スコアを合計したものとしています。
LLM に ElasticSearch の Query DSL を直接書かせることで、match による単純検索だけでなく、bool による複合条件や fuzzy によるタイポ許容検索など、柔軟な検索をできるようにしています。LLM はツールの結果を受け取って、クエリとの関連性を評価し、関連性の高いモーメントを最終的な回答として返します。ツールは LLM の判断により何度も呼び出すことができます。

LangChain の create_agent 関数を用いることでツールを持たせた ReAct エージェントを簡単に構築できます。システムプロンプトでは検索ツールを使用してよしなに対象モーメントを探すようにお願いしておきます。LLM には Claude Sonnet 4.5 を使用しました。

from langchain.agents import create_agent

SYSTEM_PROMPT = """
You are a video moment search assistant.

1. Analyze the user's query (supports Japanese)
2. Translate to English and build search queries
3. Call search_moments with the queries
4. Review results and refine if needed

Tips:
- Include synonyms (e.g., "car", "vehicle")
- For complex queries, split into sub-queries
...(省略)...
"""

agent = create_agent(model=llm, tools=[search_moments], prompt=SYSTEM_PROMPT)

result = agent.invoke({"messages": [{"role": "user", "content": "車が走っているシーン"}]})

検索できるか

勢いで構築したシステムですが、検索できるでしょうか。

まずは簡単そうなクエリ「車の運転」で試してみます。 最上位は以下のようなモーメントでした(※ 実際には動画が検索されますが、キーフレームを並べた画像で示しています)。

とりあえず動いてはいるようです。

次に「CTA」(Call To Action、広告でよく使われる視聴者に行動を促すシーン)という少し検索で引っかかりにくそうなクエリを試してみると、以下の二つのモーメントが返ってきました。


LLM のログを見ると以下のように最初に「CTA」という単語で検索し、ヒットしなかったため言い換えや関連語で再検索していることがわかりました。試行錯誤してくれています。

 [Tool Call] search_moments
    Query 1: {'match': {'caption': 'CTA'}}
  [AIMessage] The direct search for "CTA" didn't return any results. Let me try some expanded searches for what CTA might represent:
  [Tool Call] search_moments
    Query 1: {'match': {'caption': 'call to action'}}
    Query 2: {'match': {'caption': 'button click'}}
    Query 3: {'match': {'caption': 'subscribe button'}}
    Query 4: {'match': {'caption': 'sign up'}}
    ... (省略) ...
  [AIMessage] I found several video moments that relate to CTA (Call-to-Action) elements! Here are the results: ...(省略)...

より複雑で抽象的なクエリはどうなるでしょうか。
「商品のクローズアップから、企業ブランディングで終わる広告」というクエリで試してみると、以下のようなモーメントが返ってきました。

Scene 1: The video starts with a close-up of a baby bottle, then transitions to a close-up of a gold bar. Scene 2: The video ends with a close-up of a baby bottle. The video clip shows a logo of a company.
LLM は以下のようにクエリを解釈・分解して検索を試みており、面白いです。

 [Search] Query: 商品のクローズアップから、企業ブランディングで終わる広告
  [AIMessage] 商品のクローズアップから企業ブランディングで終わる広告のシーンを検索します。このような広告の典型的な流れを複数のクエリで検索してみます。
  [Tool Call] search_moments
    Query 1: {'match': {'caption': 'product close-up'}}
    Query 2: {'match': {'caption': 'product closeup'}}
    Query 3: {'match': {'caption': 'detailed product shot'}}
    Query 4: {'match': {'caption': 'company logo'}}
    Query 5: {'match': {'caption': 'brand logo'}}
    Query 6: {'match': {'caption': 'corporate branding'}}
  [AIMessage] さらに、商品から企業ロゴへの流れに焦点を当てた検索を追加で行います:
  [Tool Call] search_moments
    Query 1: {'bool': {'should': [{'match': {'caption': 'product detail'}}, {'match': {'caption': 'product shot'}}]}}
    Query 2: {'bool': {'should': [{'match': {'caption': 'logo appears'}}, {'match': {'caption': 'brand name'}}, {'match': {'caption': 'company name'}}]}}
    Query 3: {'match': {'caption': 'advertisement ending'}}
  [AIMessage] 商品のクローズアップから企業ブランディングで終わる広告のパターンを示すモーメントをいくつか発見しました。特に注目すべき例をご紹介します:...(省略)...

「日常課題を商品で解決する構成の広告」というかなり抽象度の高いクエリでは、以下のようなモーメントが返ってきました。洗濯に関する困りごとを商品で解決している流れに見えますね。

Scene 1: A man is standing in a laundry room and talking to another man. The man in the laundry room is looking at the clothes on the bed. Scene 2: A man is lying on a bed and holding a bottle of Tide laundry detergent. Scene 3: A man in a blue shirt is holding a bottle of Tide detergent and talking to another man. The man in the blue shirt is looking at the other man and smiling.
LLM は以下のようにかなりクエリを具体化して検索をしていることがわかりました。

[Search] Query: 日常課題を商品で解決する構成の広告
  [AIMessage] 日常の課題を商品で解決する構成の広告を探しますね。このタイプの広告は、問題提起→解決策提示という流れが特徴的です。
  [Tool Call] search_moments
    Query 1: {'match': {'caption': 'problem solving product'}}
    Query 2: {'match': {'caption': 'daily life challenge'}}
    Query 3: {'match': {'caption': 'before and after'}}
    Query 4: {'match': {'caption': 'solution demonstration'}}
    Query 5: {'match': {'caption': 'product benefit'}}
    Query 6: {'match': {'caption': 'household problem'}}
    Query 7: {'match': {'caption': 'cleaning solution'}}
    Query 8: {'match': {'caption': 'convenience product'}}
    Query 9: {'match': {'caption': 'time saving'}}
    Query 10: {'match': {'caption': 'difficulty struggling'}}
  [AIMessage] もう少し具体的な日常の課題解決シーンを探してみますね。
  [Tool Call] search_moments
    Query 1: {'match': {'caption': 'frustrated person'}}
    Query 2: {'match': {'caption': 'difficult task'}}
    Query 3: {'match': {'caption': 'messy kitchen'}}
    Query 4: {'match': {'caption': 'dirty clothes'}}
    Query 5: {'match': {'caption': 'tired exhausted'}}
    Query 6: {'match': {'caption': 'cleaning struggle'}}
    Query 7: {'match': {'caption': 'comparison before after'}}
    Query 8: {'match': {'caption': 'happy satisfied'}}
    Query 9: {'match': {'caption': 'easy solution'}}
    Query 10: {'match': {'caption': 'testimonial review'}}
  [AIMessage] より具体的な「課題→解決」の流れを持つ広告を探してみます。
  [Tool Call] search_moments
    Query 1: {'match': {'caption': 'stain removal'}}
    Query 2: {'match': {'caption': 'wrinkled clothes'}}
    Query 3: {'match': {'caption': 'grease dirt'}}
    Query 4: {'match': {'caption': 'easy cleaning'}}
    Query 5: {'match': {'caption': 'fast solution'}}
    Query 6: {'match': {'caption': 'washing detergent'}}
    Query 7: {'match': {'caption': 'kitchen mess'}}
    Query 8: {'match': {'caption': 'household chores'}}
    Query 9: {'match': {'caption': 'time pressure'}}
    Query 10: {'match': {'caption': 'amazing results'}}
  [AIMessage] 日常課題を商品で解決する構成の広告動画を見つけました。以下のような事例があります:...(省略)...

終わりに

見切り発車したテーマだったのですが、新しく学べたこと多くて楽しかったです。
テキスト化しているとはいえ、合計 20 時間近い動画コーパスから、任意の自然言語クエリでサクッと検索できる体験は面白く、ElasticSearch と最近の LLM の強力さを感じました。とはいえ手法としては改善の余地は多くあり、特に不正確、不十分なシーン説明文により求めていない検索結果になることもありました。
今回は厳密な比較評価ではなかったり、おそらく他にも試すべき方法があるかもしれませんが、肩の力を抜いた検証としてお楽しみいただければ幸いです。

今年もお疲れ様でした!

脚注
  1. Video Retrieval は、クエリにマッチする動画ファイルを返す設定で、Video Moment Retrieval は、あらかじめ決められた動画内のクエリにマッチするモーメント(時間区間)を返す設定です。今回扱う VCMR は、この二つを組み合わせたような設定です。 ↩︎

  2. Li et al., HERO: Hierarchical Encoder for Video+Language Omni-representation Pre-training, EMNLP 2020 ↩︎

  3. Yoon et al., Selective Query-guided Debiasing for Video Corpus Moment Retrieval, ECCV 2022 ↩︎

  4. Chen et al., VERIFIED: A Video Corpus Moment Retrieval Benchmark for Fine-Grained Video Understanding, NeurIPS 2024 ↩︎

  5. 検索を活用して AI エージェントの任意タスクにおける性能を向上させるのが Retrieval-Augmented Generation ですが、検索自体をエージェントを用いて行う技術は Agentic Retrieval と呼ばれるようです。Agentic retrieval in Azure AI Search ↩︎

  6. Long et al., AdsQA: Towards Advertisement Video Understanding, ICCV 2025 ↩︎

  7. Yao et al., ReAct: Synergizing Reasoning and Acting in Language Models, ICLR 2023 ↩︎

Discussion