dzFh3 SSKrSSKrSSKrSSKrSSKJr SSKJr SSKJr SSK J r SSK J r SSK Jr SSKJrJrJr SS KJrJr \"5 \R,"S 5r\R,"S 5r\(d\"S 5 \R4"S 5 \"SSSS9r\"SSSS9r\"SSSSSSSS9r\"S5rSrS.Sjr Sr!Sr"S r#S!r$S"r%S#r&SSK'r'S$r(S%r)S&r*S/S'jr+S0S(jr,S)r-\.S*:XaT\/"\R`5S+:a\"S,5 \R4"S 5 \-"\R`S \R`S-5 gg)1N)tqdm)Image) load_dotenv)ChatGroq)ChatPromptTemplate)pipeline) SceneManager open_videoContentDetector)SentenceTransformerutilHF_TOKEN GROQ_API_KEYu*❌ Error: HF_TOKEN not found in .env filez image-to-textz%Salesforce/blip-image-captioning-basecpu)modeldevicezvisual-question-answeringzSalesforce/blip-vqa-baseztext-generationz gpt2-mediumTg?gffffff?)rrmax_new_tokens do_sampletop_p temperaturezBAAI/bge-small-en-v1.5c/SQU-n[R"U[RS9nUR5up4URS:wa1[ SUR 55 [R"S5 gg)N)ffmpegz -hide_bannerz -loglevelerrorz-y)stderrru❌ FFmpeg error: r) subprocessPopenPIPE communicate returncodeprintdecodesysexit)cmdfullp_errs .C:\Users\Zamal Ali\Downloads\videoxity\main.py run_ffmpegr,5s\ AC GDjoo6A ]]_FA||q #SZZ\2  c[U5n[5nUR[US95 UR U5 UR 5$)N) threshold)r r add_detectorr detect_scenesget_scene_list) video_paththreshvmgrs r+r1r1@sC:A .C_v67a    r-c[SSSS9n[R"SS/5nX2-nSRS[ U555nUR XS .5n[ URR55n[U[5(a[S U55(d [S 5eU$![a%n[S WR35 /nSnAU$SnAff=f) Nllama-3.1-8b-instant皙?rrr max_tokens)systemuYou are a helpful assistant for video analysis. The user will give you a list of scene captions, each labeled with an index like [1], [2], ..., and a filtering instruction like 'remove food scenes'. Return ONLY the list of indexes that should be removed — e.g., [2, 5, 9] ⚠️ Do not explain, describe, or add any commentary. Your response MUST be a valid Python list of integers.)humanz4Filtering instruction: {query} Captions: {captions} c3X# UH upSUS-SUR53v M" g7f[rz] Nstrip.0icaps r+ +get_removal_indices_groq.._+"^J]Qqse2ciik]#;J](*)querycaptionsc3B# UHn[U[5v M g7f)N) isinstanceint)rErFs r+rHrIes5\R[QjC6H6HR[szInvalid formatu!❌ LLM returned invalid output: )rr from_messagesjoin enumerateinvokeevalcontentrCrOlistall ValueError Exceptionr") rMrLllmpromptchaincaptions_formattedresponse to_removees r+get_removal_indices_groqrbIs $ C  - -   / F LE"^)T\J]"^^ <<% PQ))//12 )T**#5\R[5\2\2\-. .3]   1(2B2B1CDE  s A,B88 C'C""C'cj[SSSS9nURU5RR5$)Nr8r9rr:)rrTrVrC)r\r[s r+groq_llmrdos5 $ C ::f  % % + + --r-cL[R"U5/p2UHvupEUR5UR5-S-nUR[RU5 UR 5upxU(dMdUR Xh45 Mx UR5 U$)N)cv2 VideoCapture get_framessetCAP_PROP_POS_FRAMESreadappendrelease) r3scenesrGframessramidokimgs r+extract_keyframesruys~"":.||~ .14 ''-((* 2v}}cZ(  KKM Mr-c[R"[R"U[R55n[ U5SS$)Nrgenerated_text)r fromarrayrgcvtColor COLOR_BGR2RGB captioner)framerts r+generate_scene_captionr}s6 //#,,uc.?.?@ AC S>! - ..r-c [SSSS9n[R"SS/5nX!-nSRS[ U555nUR S U05nUR R5nS US [U5S [S 5RSUSS55S3$![an[SU35 SnAgSnAff=f)z(Generate a video summary using Groq LLM.r8r9rr:)r<aDYou are a helpful assistant for video analysis. The user will give you a list of scene captions from a video. Your task is to write a concise, narrative summary of what happens in the video, focusing only on the events shown. Make it engaging and easy to understand. Do not include any titles, links, or external references.)r=zdHere are the scene captions from the video in order: {captions} Please provide a narrative summary.r>c3X# UH upSUS-SUR53v M" g7fr@rBrDs r+rH.generate_video_summary_groq..rJrKrMu🎬 Video Summary: u 📊 Total Scenes: u 🔍 Key Moments: c3,# UH nSU3v M g7f)u• N)rErGs r+rHrs 3lsSEllsNz ...u(❌ Error generating summary with Groq: u+❌ Error: Failed to generate video summary) rrrQrRrSrTrVrClenchrrZr")rMr[r\r]r^r_summaryras r+generate_video_summary_groqrs $ C  - -   / F LE"^)T\J]"^^=<<-? @A""((*  M?#R 3hrl 3345   = 8<=<=s A"B,, C6C  Cc[U5$)z* Generate a video summary using Groq LLM. )r)rMs r+generate_video_summaryrs 'x 00r-c DSR[U5VVs/sHup4US-SU3PM snn5nSUSUS3nU"U5n[R"UR 55nUVs/sHo3S- PM nnU$s snnfs snf! [ SU5 /s$=f)aX Uses an LLM to determine which scenes to remove based on captions and a user query. Args: captions (List[str]): List of scene/frame captions. query (str): User intent, e.g. "Remove scenes with Trump". llm (callable): Function to call your LLM, e.g. `llm(prompt)`. Returns: List[int]: List of 0-based frame indexes to remove. r>rz. z> You're an intelligent video assistant. The user wants to: **z;** Below are numbered captions for each scene in a video: u 👉 Return a Python list of only the scene numbers that should be removed based on the user query. 👉 ONLY return the list like this: [3, 5, 11]. No explanation. u"⚠️ Failed to parse LLM output:)rRrSast literal_evalrCr") rMrLr[rFrG formattedr\r_results r+filter_scenes_with_llmrs 89LM9LvqacU"SE?9LMNIW   F6{H!!(.."23%&v!A#v& 'N$' 2H= s#B 'B 0B>B B BcU(d/$[U5nUS//nUSSH9nX!SSS-:XaUSRU5 M'URU/5 M; U$)z-Group consecutive indices together as chunks.rrN)sortedrm)indicesgroupsrFs r+ group_indicesrsj  WoGqzl^F QR[ r 2" " 2J  a MM1#   Mr-c/nUHsup4[R"[R"U[R55n[ XQS.5nUR SUSSR5;5 Mu U$)N)imagequestionyesranswer)rrxrgryrz vl_pipelinermlower) keyframesrflagsr)r|rtanss r+ vqa_matchesrsi Eoocll5#2C2CDEC>? Uc!fX.44667 Lr-c[RUSS9n[RUSS9n[R"XC5Sn[ U5VVs/sHupgXr:dM UPM snnUR 54$s snnf)NT)convert_to_tensorr)embedderencoder cos_simrStolist)rMr\r4embsqsimsrFrqs r+semantic_matchesrsf ??8t? A+=>A#&&q)? u:. .  " "5 ) HHY Z[#5) F)#!Q-EA)D FKK$/.&"4IAgr"o!D/ABB js2ws2w 8\   LL )$+s #q&3($  ( S + I x D E F    Aww~~a   !  77>>+ & & IIk " 77>>/ * * HH_ % +U Q. G4$ #(Aww~~a   !  77>>+ & & IIk " 77>>/ * * HH_ % +s=>J") J(8J(B J?"J.&J?. J<8J??(M;+BM;c[SUSUS35 [U5n[S[U5S35 [X5n[S[U5S35 [ USS 9VVs/sHupE[ U5PM nnn[ U5n[S 5 [U5 [Xa[5n[S U35 U(a[XU5 [S 5 U$[S 5 U$s snnf)Nu 🎥 Video: u 🔎 Query: 'ru🔢 z scenes detected.u🖼️ z keyframes extracted. zGenerating captions)descz --- Video Summary ---u 🔴 Scenes to remove: u)✅ Trimmed video saved as `trimmed.mp4`.u2⚠️ No matching scenes found; no trimming done.) r"r1rrurr}rrrdr) videorLrorr)rrMrr`s r+runrTs N5's ;<e$F E#f+/ 01!%0I HS^$$; <=6:9K`6ab6ada&q)6aHb%h/G #$ 'N'xAI %i[ 12eY/ 9:  BC cs%C+__main__rz3Usage: python main.py "your query here"rf)g.@)g?)z trimmed.mp4)1rr$rgrrPILrdotenvrlangchain_groqrlangchain_core.promptsr transformersr scenedetectr r r sentence_transformersr r getenvrrr"r%r{r elaboratorrr,r1rbrdrur}rrrrrrrrr__name__rargvrr-r+rs[ #5!AA;  99Z yy(  67HHQK  1    $       7 8  #L./'=R1 "J FL&b8 z 388}Q EF   SXXa[! r-