2012-11-18 36 views
3

如何在Android中將MPEG4數據寫入(包裝)成MP4文件?Android上的視頻處理,如何將MPEG4數據寫入MP4文件?

我做Android平臺的某種視頻處理,但我不知道怎麼寫處理的數據(在某種標準編碼,如MPEG4)回的MP4的視頻文件。我認爲最好使用API​​來做到這一點,但我找不到所需的API。

有沒有人有任何想法?

回答

0

OpenCV可能是有點吃不消了工作,但我想不出什麼更容易。 OpenCV是一個爲C,C++和Python提供API的計算機視覺庫。

由於您使用的是Android,你必須下載OpenCV的Java包裝命名爲JavaCV,這是一個第三方的API。我寫了一個instructions to install OpenCV/JavaCV on Windows的小貼子,並將它與Netbeans一起使用,但最終你必須搜索一個教程來展示如何爲Android平臺安裝OpenCV/JavaCV。

This is a C++ example展示瞭如何打開一個輸入視頻和幀複製到輸出文件。但是,由於您使用的是Android的使用JavaCV是更好的一個例子,所以從輸入視頻幀下面的代碼拷貝,並將其寫入名爲out.mp4的輸出文件:在幀OpenCV的商店像素:

package opencv_videowriter; 

import static com.googlecode.javacv.cpp.opencv_core.*; 
import static com.googlecode.javacv.cpp.opencv_imgproc.*; 
import static com.googlecode.javacv.cpp.opencv_highgui.*; 

public class OpenCV_videowriter 
{ 
    public static void main(String[] args) 
    { 
     CvCapture capture = cvCreateFileCapture("cleanfish47.mp4"); 
     if (capture == null) 
     { 
      System.out.println("!!! Failed cvCreateFileCapture"); 
      return; 
     } 

     int fourcc_code = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FOURCC); 
     double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); 
     int w = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); 
     int h = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); 

     CvVideoWriter writer = cvCreateVideoWriter("out.mp4",  // filename 
                fourcc_code, // video codec 
                fps,   // fps 
                cvSize(w, h), // video dimensions 
                1);    // is colored 
     if (writer == null) 
     { 
      System.out.println("!!! Failed cvCreateVideoWriter"); 

      return; 
     } 


     IplImage captured_frame = null;   
     while (true) 
     { 
      // Retrieve frame from the input file 
      captured_frame = cvQueryFrame(capture); 
      if (captured_frame == null) 
      { 
       System.out.println("!!! Failed cvQueryFrame"); 
       break; 
      } 


      // TODO: write code to process the captured frame (if needed) 


      // Store frame in output file 
      if (cvWriteFrame(writer, captured_frame) == 0) 
      { 
       System.out.println("!!! Failed cvWriteFrame"); 
       break; 
      } 

     } 

     cvReleaseCapture(capture); 
     cvReleaseVideoWriter(writer);   
    } 
} 

注意訂單號爲BGR

+0

感謝您提供幫助。 – guanlin

+1

根據這個SO問題(http://stackoverflow.com/questions/21546906/how-to-open-cvvideowriter-in-android)「OpenCV4adnroid不支持視頻閱讀和寫作」。它從android實現的OpenCV中缺失。 – Mick

1

你的問題沒有100%的意義。 MPEG-4是規範族(所有ISO/IEC 14496- *),MP4是ISO/IEC 14496-14中規定的文件格式。

如果你想創建一個從原始的AAC和/或H264流,我建議使用mp4parser庫中的MP4文件。有一個example,顯示如何將AAC和H264複合到MP4文件中。

2

mp4parser可以完全創建碼流,只有工作,ü不能用它寫幀幀。糾正我,如果我錯了

H264TrackImpl h264Track = new H264TrackImpl(new BufferedInputStream(some input stream here)); 
Movie m = new Movie(); 
IsoFile out = new DefaultMp4Builder().build(m); 
File file = new File("/sdcard/encoded.mp4"); 
FileOutputStream fos = new FileOutputStream(file); 
out.getBox(fos.getChannel()); 
fos.close(); 

現在我們需要知道如何寫幀逐幀。

相關問題