[Tex/LaTex] Embedding a flv (or mp4) video using pdfTeX (plain TeX only)

animationsembeddingpdfpdftexplain-tex

I am attempting to find out how to embed a flv video in a pdf file created using plain TeX and pdfTeX. I was successful at manually (via Adobe Acrobat's menu: Tools->Multimedia->Video Tool) embedding a flv video clip into an existing pdf. Then examined the pdf file in a text editor (vim) to see if I could figure out the code that needed to be added to embed a video. Although I was able to identify some of the lines associated with the embedding, I am clueless as to how to achieve the embedding using plain TeX and pdfTeX. Would appreciate any pointers.

Thanks.

PS: @AlexG 's response does give me what I was looking for. However, I do have a follow up question. I attempted to write the follow-up as a comment, but was not able to format the follow-up properly. So here it is:

Toward the end of the code, what's the need for the "\setbox0=…" and the "\hbox to …" lines? Commenting these two lines did not seem to affect the solution:

top  

%\setbox0=\hbox{  }\immediate\pdfxform 0%  

\vbox to 25.6mm{%  
\pdfannot width \videowidth depth \videoheight height 0pt {  
  /Subtype/RichMedia  
  /BS <</W 0/S/S>>  
  /Contents (embedded video file:\videofile)  
  /NM (rma:\videofile)  
  /AP <</N \the\pdflastxform\space 0 R>>  
  /RichMediaSettings \the\pdflastobj\space 0 R  
  /RichMediaContent \rmcontent\space 0 R  
}}%  
%\hbox to \videowidth {\vbox to \videoheight {}\hss}%  

bottom  

The above ending results in the video showing up placed vertically between the words top and bottom.

Best Answer

The following example defines \embedvideo. It uses the same media embedding method as the media9 LaTeX package. Video files in the MP4/H264 and FLV formats are played back within the video player component VPlayer.swf shipping with media9.

See the media9 manual on how to configure VPlayer via FlashVars, which are passed as the 3rd argument of \embedvideo.

The video player can be controlled by left mouse button press (pause) and release (play/resume), via the context menu accessed by right mouse button click and via the keyboard (see context menu for keys).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%             
% \embedvideo{<width>}{<height>}{<flashvars>}{<video file (MP4+H264 or FLV)>}              
% flashvars documented in `texdoc media9`,  page 19                                        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%             
\def\embedvideo#1#2#3#4{%                                                                  
  \ifdefined\vplayer\else%                                                                 
    \immediate\pdfobj stream file {VPlayer.swf}%                                           
    \immediate\pdfobj{<<                                                                   
      /Type/Filespec/F (VPlayer.swf)/UF (VPlayer.swf)                                      
      /EF <</F \the\pdflastobj\space 0 R>>                                                 
    >>}%                                                                                   
    \edef\vplayer{\the\pdflastobj}%                                                        
  \fi%                                                                                     
  %                                                                                        
  \immediate\pdfobj stream file {#4}%                                                      
  \immediate\pdfobj{<<                                                                     
    /Type/Filespec/F (#4)/UF (#4)                                                          
    /EF <</F \the\pdflastobj\space 0 R>>                                                   
  >>}%                                                                                     
  \edef\video{\the\pdflastobj}%                                                            
  %                                                                                        
  \immediate\pdfobj{<<                                                                     
    /Type/RichMediaInstance/Subtype/Video                                                  
    /Asset \vplayer\space 0 R                                                              
    /Params <</Binding/Foreground/FlashVars (source=#4&#3)>>                               
  >>}%                                                                                     
  %                                                                                        
  \immediate\pdfobj{<<                                                                     
    /Type/RichMediaConfiguration/Subtype/Video                                             
    /Instances [\the\pdflastobj\space 0 R]                                                 
  >>}%                                                                                     
  %                                                                                        
  \immediate\pdfobj{<<                                                                     
    /Type/RichMediaContent                                                                 
    /Assets <<                                                                             
      /Names [(VPlayer.swf) \vplayer\space 0 R (#4) \video\space 0 R]                      
    >>                                                                                     
    /Configurations [\the\pdflastobj\space 0 R]                                            
  >>}%                                                                                     
  \edef\rmcontent{\the\pdflastobj}%                                                        
  %                                                                                        
  \pdfobj{<<                                                                               
    /Activation <<                                                                         
      /Condition/PO                                                                        
      /Presentation <<                                                                     
        /Transparent true                                                                 
        /Style/Embedded                                                                    
        /PassContextClick true                                                            
      >>                                                                                   
    >>                                                                                     
    /Deactivation <</Condition/PC>>                                                        
  >>}%                                                                                     
  %                                                                                        
  \setbox0=\vbox to #2 {\vss\hbox to #1{\tt #4\hss}\vss}%
  \immediate\pdfxform 0%                                              
  %                                                                                        
  \pdfannot width #1 height #2 depth 0pt {                                                 
    /Subtype/RichMedia                                                                     
    /BS <</W 0/S/S>>                                                                       
    /Contents (embedded video file:#4)                                                     
    /NM (rma:#4)                                                                           
    /AP <</N \the\pdflastxform\space 0 R>>                                                 
    /RichMediaSettings \the\pdflastobj\space 0 R                                           
    /RichMediaContent \rmcontent\space 0 R                                                 
  }%                                                                                       
  \hbox to #1 {\vbox to #2 {}\hss}%                                                        
}%                                                                                         
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%             

Plain\TeX{} video example:                                                                

top

left\embedvideo{4cm}{3cm}{autoPlay=true&loop=true}{random.mp4}right                        

bottom
\bye

To answer the follow-up question:

The lines \setbox0=... and \immediate\pdfxform 0% produce a poster text for the inactive video. The text (video file name) is put in a box and subsequently distilled into a so-called Form-XObject. The latter is bound to the RichMedia annotation using the /AP entry. Without a poster text, AR displays an ugly box with a "?" inside if the video is inactive.

Related Question