Skip to main content

Building a RAG Pipeline

Introduction​


RAG applied to documents is typically described in three stages.

  1. Read the document to understand its structure (Parsing)
  2. Split the document into meaningful units and convert them into vectors (Chunking · Embedding)
  3. Find chunks similar to the question and generate an answer (Retrieval · Generation)

For documents, the process ends in the final step by quoting relevant paragraphs. Broadcast videos are different. The answer the user wants is not a paragraph but a segment—specified as “from minute X, second Y of episode Z to second W”—and the final result is a video composed of those segments strung together.

image

That’s why SceneMaker has split RAG into two parts: the indexing side, which analyzes and stores video clips, and the scheduling side, which receives queries and extracts intervals. The key difference from document-based RAG is that both sides attach start and end timestamps to every result throughout the entire process.


1. Overall Structure​

When a video is uploaded, it is processed in the following order.

image

  • Subtitles: Speech is transcribed to identify dialogue and speakers, and a plot summary is generated in one-minute intervals. Transcription is handled by the Qwen3-ASR model, while correction and summarization are performed by the Qwen 27B model.
  • Frame Analysis and Indexing: The video is divided into cuts; each cut’s frame content is analyzed; these are grouped into scenes and sequences; and finally, they are indexed as vectors. RAG’s parsing, chunking, and embedding all occur at this stage.
  • Compilation: Given a single line of text as a query and the desired duration, the system finds matching scenes, cuts out the segments, and creates a list of clip segments. This corresponds to the search and generation components of RAG.
  • Video Composition: After reviewing the list of clip segments on screen and requesting composition, an MP4 file is generated by cutting and splicing the segments together.

The analysis results are stored in two locations. The analysis database contains the time, location, characters, and summary for each cut, scene, sequence, and line of dialogue, while the vector repository stores the corresponding text entries as vectors. Entries retrieved via vector search include the corresponding timestamps, allowing them to be directly converted into video segments.


2. Indexing: Cutting into Small Pieces, Grouping into Larger Units​

The indexing process involves cutting the video into the smallest possible units and then gradually grouping them into larger units.

image

  1. Cut Division: Cuts are created at every point where the screen changes. This is the smallest unit of the video.
  2. Sequence Draft: Based on the 1-minute plot summaries created during the subtitling phase, as well as the episode outline and character list, the episode is divided into larger blocks organized by events. Since these are 1-minute units, the boundaries are still rough.
  3. Cut Analysis: For each cut, review 1–3 frames to identify the type (dialogue, action, etc.), location, characters, and provide a one-line description. Since it is difficult to understand the situation from a single cut, provide the sequence summary created earlier along with the dialogue overlapping that cut and the facial recognition results.
  4. Sequence Adjustment: Based on the cut analysis results, revise the sequence boundaries, titles, and summaries. If a sequence is too large, split it up.
  5. Scene Grouping: For each sequence, group the cuts within it into scenes based on the same time and location, and assign a location, characters, and summary to each scene.
  6. Indexing: Store all four units—sequences, scenes, cuts, and dialogue—in a single vector repository. For each item, store the start and end times, location, and characters together; only text elements such as titles, summaries, one-line descriptions, and dialogue are vectorized using Qwen3-Embedding.

The reason for including all four units is that the appropriate size varies depending on the query. A query like “the scene where two people first meet” might match a scene or sequence summary, while a specific line of dialogue would match the dialogue unit. Regardless of which unit the match occurs in, it returns a segment on the same timeline, so the editing team can handle the segment regardless of the unit that triggered the match.


3. Editing: Search, Cut, and Align​

The editing team takes a single line of the query and the required duration to create a list of clip segments. When working across multiple episodes, they run the same process simultaneously for each episode and then consolidate the results.

image

For each episode, the process proceeds in the following order.

  1. Interpreting the Query: Determine whether the query requests a plot summary, a search for a specific scene, or some other type of request. Conditions such as characters, locations, search terms, duration, and number of clips are also identified at this stage. Characters and locations are selected only from names that actually appear in that specific episode.
  2. Gathering Candidates: For a summary, all scenes from the main episode are considered; for a search, scenes matching the characters and locations, along with those identified by vector search, are combined as candidates. The model is not used at this stage.
  3. Scene Selection: For each sequence, the system reads all the scene descriptions and dialogue from the candidate scenes and selects the scene that best matches the query.
  4. Segment Cutting: The scene descriptions and dialogue from the selected scenes are combined on a single timeline, with each line numbered; the model selects only the start and end line numbers. The code converts the line numbers into seconds. If no matching segment is found, it is excluded rather than forced to fill the gap.
  5. Ranking: Exclude any clips from the selected set that do not match the query, and rank the remaining clips by importance.

After collecting the results from all episodes, proceed as follows:

  • Finalization: Determine the overall ranking across all rounds and merge overlapping clips. The code enforces quantity constraints—such as “3 in total” or “1 per round”—rather than relying on the model to count them. When the model was tasked with counting, it sometimes omitted episodes or selected two clips from a single episode.
  • Adjusting Duration: If the total duration exceeds the requested length, the target time is allocated proportionally to the original length, starting with the highest-ranked clips, and only the key segments within each clip are reselected. If it still exceeds the limit, clips are removed starting with the lowest-ranked ones. Unselected segments are not pulled in to make up the difference.
  • Sorting: The remaining clips are sorted by playback order (episode order, chronological order) to create a list of clip segments.

Candidate selection and duration allocation are handled solely by code, while the Qwen 27B model handles all other decisions. The fact that the model selects only line numbers rather than specifying the exact duration in seconds, and that the code manages the count, are both results of shifting tasks where the model is prone to errors to the code.


Conclusion​


Currently, this structure spans the entire process from video registration to subtitling, video analysis and indexing, scheduling, and video compositing. The video data here is not used to train the model; it is simply indexed to enable searching.

How each stage of the scheduling process is connected will be covered in the next article (“Designing a LangGraph-Based Broadcasting Workflow”).

This article presents research results supported by the Ministry of Science and ICT and the National IT Industry Promotion Agency through the “2026 Open-Source AI and Software Development and Utilization Support Project.”