JAVA PPT转MP4 (基于VBSCRIPT)

场景:

业务需要,要使用JAVA根据ppt模板自动生成MP4视频。

调研:

查了一下比较符合的PPT 操作方式,POI,ASPOSE,VBA

1. POI 支持PPT文件的读,修改,写。HSLF,XLSF支持PNG,以及基于Graphics2D 的输入文件格式,但没见到有视频输出的。 详见:XLSF

2. ASPOSE 支持的输出文件相对多一些 详见:outputFormat,但是,很遗憾,不支持MP4类视频文件。

3. VBSCRIPT 是通用的 但是不能直接调用,需要 JAVA调用VBSCRIPT导出文件

实操作:

没办法,只能VBSCRIPT了。

调用过程是这样:JAVA->CSCRIPT->执行vbscript脚本->输出MP4

JAVA:

[java]  view plain  copy
  1. public void testNewPPT() throws IOException, InterruptedException {  
  2.     //注意“E:/Temp/vbs.vbs”应该是正确的存储的VBS代码的位置  
  3.     Process process = Runtime.getRuntime().exec("cmd /c CScript E:/example2.vbs");  
  4.     //等待VBS执行完毕  
  5.     process.waitFor();  
  6.   
  7. }  

VBSCRIPT(参考Neohope's Blog)

[vb]  view plain  copy
  1. Option Explicit  
  2.    
  3. 'PPT2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.pptx","PATH_TO_INFILE\NEOHOPE.COM.OUT.pdf","PDF"  
  4. 'PPT2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.pptx","PATH_TO_INFILE\NEOHOPE.COM.OUT.png","PNG"  
  5. PPT2ANY "e:\example2.pptx","e:\777","MP4"  
  6.    
  7. Sub PPT2ANY( inFile, outFile, outFormat)  
  8.     Dim objFSO, objPPT, objPresentation, pptFormat  
  9.    
  10.     Const ppSaveAsAddIn                             =8  
  11.     Const ppSaveAsBMP                               =19  
  12.     Const ppSaveAsDefault                           =11  
  13.     Const ppSaveAsEMF                               =23  
  14.     Const ppSaveAsExternalConverter                 =64000  
  15.     Const ppSaveAsGIF                               =16  
  16.     Const ppSaveAsJPG                               =17  
  17.     Const ppSaveAsMetaFile                          =15  
  18.     Const ppSaveAsMP4                               =39  
  19.     Const ppSaveAsOpenDocumentPresentation          =35  
  20.     Const ppSaveAsOpenXMLAddin                      =30  
  21.     Const ppSaveAsOpenXMLPicturePresentation        =36  
  22.     Const ppSaveAsOpenXMLPresentation               =24  
  23.     Const ppSaveAsOpenXMLPresentationMacroEnabled   =25  
  24.     Const ppSaveAsOpenXMLShow                       =28  
  25.     Const ppSaveAsOpenXMLShowMacroEnabled           =29  
  26.     Const ppSaveAsOpenXMLTemplate                   =26  
  27.     Const ppSaveAsOpenXMLTemplateMacroEnabled       =27  
  28.     Const ppSaveAsOpenXMLTheme                      =31  
  29.     Const ppSaveAsPDF                               =32  
  30.     Const ppSaveAsPNG                               =18  
  31.     Const ppSaveAsPresentation                      =1  
  32.     Const ppSaveAsRTF                               =6  
  33.     Const ppSaveAsShow                              =7  
  34.     Const ppSaveAsStrictOpenXMLPresentation         =38  
  35.     Const ppSaveAsTemplate                          =5  
  36.     Const ppSaveAsTIF                               =21  
  37.     Const ppSaveAsWMV                               =37  
  38.     Const ppSaveAsXMLPresentation                   =34  
  39.     Const ppSaveAsXPS                               =33  
  40.    
  41.     ' Create a File System object  
  42.     Set objFSO = CreateObject( "Scripting.FileSystemObject" )  
  43.    
  44.     ' Create a PowerPoint object  
  45.     Set objPPT = CreateObject( "PowerPoint.Application" )  
  46.    
  47.     With objPPT  
  48.         ' True: make PowerPoint visible; False: invisible  
  49.         .Visible = True  
  50.     
  51.         ' Check if the PowerPoint document exists  
  52.         If not( objFSO.FileExists( inFile ) ) Then  
  53.             WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf  
  54.             ' Close PowerPoint  
  55.             .Quit  
  56.             Exit Sub  
  57.         End If  
  58.     
  59.         ' Open the PowerPoint document  
  60.         .Presentations.Open inFile  
  61.     
  62.         ' Make the opened file the active document  
  63.         Set objPresentation = .ActivePresentation  
  64.     
  65.         If StrComp(Ucase( outFormat ),"PDF") = 0 then  
  66.             pptFormat = ppSaveAsPDF   
  67.         ElseIf StrComp(Ucase( outFormat ),"XPS") = 0 then  
  68.             pptFormat = ppSaveAsXPS  
  69.         ElseIf StrComp(Ucase( outFormat ),"BMP") = 0 then  
  70.             pptFormat= ppSaveAsBMP  
  71.         ElseIf StrComp(Ucase( outFormat ),"PNG") = 0 then  
  72.             pptFormat= ppSaveAsPNG  
  73.         ElseIf StrComp(Ucase( outFormat ),"JPG") = 0 then  
  74.             pptFormat= ppSaveAsJPG  
  75.         ElseIf StrComp(Ucase( outFormat ),"GIF") = 0 then  
  76.             pptFormat= ppSaveAsGIF  
  77.         ElseIf StrComp(Ucase( outFormat ),"XML") = 0 then  
  78.             pptFormat= ppSaveAsOpenXMLPresentation  
  79.         ElseIf StrComp(Ucase( outFormat ),"RTF") = 0 then  
  80.             pptFormat= ppSaveAsRTF  
  81.         ElseIf StrComp(Ucase( outFormat ),"MP4") = 0 then  
  82.             pptFormat= ppSaveAsMP4  
  83.         Else  
  84.             WScript.Echo "FILE FORTMART ERROR: Unknown file format" & vbCrLf  
  85.             ' Close PowerPoint  
  86.             .Quit  
  87.             Exit Sub  
  88.         End If  
  89.    
  90.         ' Save in PDF/XPS format  
  91.         objPresentation.SaveAs outFile, pptFormat  
  92.         ' save as MP4 need long time, do not close too early  
  93.         If StrComp(Ucase( outFormat ),"MP4") = 0 then  
  94.             wscript.sleep 1000*15  
  95.         End If  
  96.         ' Close the active document  
  97.         objPresentation.Close  
  98.     
  99.         ' Close PowerPoint  
  100.         .Quit  
  101.     End With  
  102. End Sub  

详见:文件键值对对应关系

注意:

1 因为要用到vbscript,部署环境需要windows。

2 saveas 格式为 ppSaveAsMP4或其它较大输出文件时,要注意根据文件大小,设定sleep时间,正在进行导出时执行了close 是获取不到输出文件的。

3 导出时PPT会打开。

4 PPT最好是英文名,否则需要处理乱码问题。


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值