Search in sources :

Example 1 with AudioClip

use of org.storymaker.app.model.AudioClip in project storymaker by StoryMaker.

the class MediaProjectManager method doExportMedia.

public MediaDesc doExportMedia(File fileExport, boolean doCompress, boolean doOverwrite) throws Exception {
    MediaDesc mMediaDescOut = null;
    Message msg = mHandler.obtainMessage(0);
    msg.getData().putString("status", "cancelled");
    ArrayList<Media> mList = mProject.getMediaAsList();
    ArrayList<MediaDesc> alMediaIn = new ArrayList<MediaDesc>();
    Utils.Proc.killZombieProcs(mContext);
    double storageSpace = checkStorageSpace();
    //if not enough space
    if (storageSpace > 0) {
        // throw exception to pass info up the stack
        throw new Exception(String.format(mContext.getString(R.string.error_storage_space), storageSpace));
    }
    File fileRenderTmpDir = getRenderPath(mContext);
    File fileRenderTmp = new File(fileRenderTmpDir, new Date().getTime() + "");
    fileRenderTmp.mkdirs();
    //for video, render the sequence together
    if (mProject.getStoryType() == Project.STORY_TYPE_VIDEO) {
        int mIdx = 0;
        for (Media media : mList) {
            if (media != null) {
                MediaDesc mediaDesc = new MediaDesc();
                mediaDesc.mimeType = media.getMimeType();
                mediaDesc.path = new File(media.getPath()).getCanonicalPath();
                if (media.getTrimStart() > 0) {
                    mediaDesc.startTime = "" + media.getTrimmedStartTimeFloat() / 1000F;
                    mediaDesc.duration = "" + media.getTrimmedDuration() / 1000F;
                } else if ((media.getTrimEnd() < 99) && media.getTrimEnd() > 0) {
                    mediaDesc.duration = "" + media.getTrimmedDuration() / 1000F;
                }
                mediaDesc.audioVolume = media.getVolume();
                if (doCompress)
                    applyExportSettings(mediaDesc);
                applyExportSettingsResolution(mediaDesc);
                alMediaIn.add(mIdx, mediaDesc);
                mIdx++;
            }
        }
        mMediaDescOut = new MediaDesc();
        if (doCompress)
            applyExportSettings(mMediaDescOut);
        else //this is the default audio codec settings
        {
            mMediaDescOut.audioCodec = "aac";
            mMediaDescOut.audioBitrate = 64;
        }
        applyExportSettingsResolution(mMediaDescOut);
        //override for now
        mMediaDescOut.mimeType = AppConstants.MimeTypes.MP4;
        mMediaDescOut.path = fileExport.getCanonicalPath();
        if ((!fileExport.exists()) || doOverwrite) {
            if (fileExport.exists()) {
                fileExport.delete();
            }
            fileExport.getParentFile().mkdirs();
            //there can be only one renderer now - MP4Stream !!
            MediaVideoExporter mEx = new MediaVideoExporter(mContext, mHandler, alMediaIn, fileRenderTmp, mMediaDescOut);
            //			    	if (audioPath != null)
            for (AudioClip audioClip : mProject.getAudioClipsAsList()) {
                // TODO truncate and pad the font of and set volume on this clip
                String tempPath = audioClip.getPath();
                MediaDesc audioTrack = new MediaDesc();
                audioTrack.path = tempPath;
                // negative time will be padded in export
                audioTrack.startTime = audioClip.getStartTime();
                audioTrack.duration = audioClip.getDuration();
                audioTrack.audioVolume = audioClip.getVolume();
                // TODO put volume in here
                mEx.addAudioTrack(audioTrack);
            }
            mEx.export();
        }
    } else if (mProject.getStoryType() == Project.STORY_TYPE_AUDIO) {
        int mIdx = 0;
        for (Media media : mList) {
            if (media != null) {
                MediaDesc mDesc = new MediaDesc();
                mDesc.mimeType = media.getMimeType();
                mDesc.path = media.getPath();
                if (media.getTrimStart() > 0) {
                    mDesc.startTime = "" + media.getTrimmedStartTimeFloat() / 1000F;
                    mDesc.duration = "" + media.getTrimmedDuration() / 1000F;
                } else if ((media.getTrimEnd() < 99) && media.getTrimEnd() > 0) {
                    mDesc.duration = "" + media.getTrimmedDuration() / 1000F;
                }
                mDesc.audioVolume = media.getVolume();
                if (doCompress)
                    applyExportSettings(mDesc);
                applyExportSettingsResolution(mDesc);
                alMediaIn.add(mIdx++, mDesc);
            }
        }
        mMediaDescOut = new MediaDesc();
        // 		    mOut.mimeType = AppConstants.MimeTypes.OGG;
        //		    mOut.mimeType = AppConstants.MimeTypes.MP4_AUDIO;
        mMediaDescOut.mimeType = AppConstants.MimeTypes.THREEGPP_AUDIO;
        // if (doCompress)
        applyExportSettingsAudio(mMediaDescOut);
        applyExportSettingsResolution(mMediaDescOut);
        mMediaDescOut.path = fileExport.getCanonicalPath();
        if ((!fileExport.exists()) || doOverwrite) {
            if (fileExport.exists())
                fileExport.delete();
            fileExport.getParentFile().mkdirs();
            MediaAudioExporter mEx = new MediaAudioExporter(mContext, mHandler, alMediaIn, fileRenderTmp, mMediaDescOut);
            mEx.export();
        }
    } else if (mProject.getStoryType() == Project.STORY_TYPE_PHOTO) {
        for (Media media : mList) {
            if (media == null)
                continue;
            MediaDesc mDesc = new MediaDesc();
            mDesc.mimeType = media.getMimeType();
            mDesc.path = media.getPath();
            if (mDesc.path != null) {
                File fileSrc = new File(mDesc.path);
                if (fileSrc.exists()) {
                    fileExport.getParentFile().mkdirs();
                    fileExport.createNewFile();
                    IOUtils.copy(new FileInputStream(fileSrc), new FileOutputStream(fileExport));
                    mMediaDescOut = new MediaDesc();
                    mMediaDescOut.path = fileExport.getCanonicalPath();
                    mMediaDescOut.mimeType = AppConstants.MimeTypes.JPEG;
                    applyExportSettingsResolution(mMediaDescOut);
                    break;
                }
            }
        }
    } else if (mProject.getStoryType() == Project.STORY_TYPE_ESSAY) {
        for (Media media : mList) {
            if (media != null) {
                MediaDesc mDesc = new MediaDesc();
                mDesc.mimeType = media.getMimeType();
                File fileSrc = new File(media.getPath());
                File fileTmp = new File(fileRenderTmp, fileSrc.getName());
                if (!fileTmp.exists()) {
                    fileTmp.getParentFile().mkdirs();
                    fileTmp.createNewFile();
                    IOUtils.copy(new FileInputStream(fileSrc), new FileOutputStream(fileTmp));
                }
                mDesc.path = fileTmp.getCanonicalPath();
                if (doCompress)
                    applyExportSettings(mDesc);
                applyExportSettingsResolution(mDesc);
                alMediaIn.add(mDesc);
            }
        }
        mMediaDescOut = new MediaDesc();
        if (doCompress)
            applyExportSettings(mMediaDescOut);
        applyExportSettingsResolution(mMediaDescOut);
        mMediaDescOut.path = fileExport.getCanonicalPath();
        mMediaDescOut.mimeType = AppConstants.MimeTypes.MP4;
        int slideDuration = Integer.parseInt(mSettings.getString("pslideduration", AppConstants.DEFAULT_SLIDE_DURATION + ""));
        String audioPath = null;
        File fileAudio = new File(getExternalProjectFolder(mProject, mContext), "narration" + mScene.getId() + ".wav");
        if (fileAudio.exists())
            audioPath = fileAudio.getCanonicalPath();
        else {
            // fileAudio = new File(mContext.getExternalFilesDir(null),"narration" + mScene.getId() + ".wav");
            fileAudio = new File(StorageHelper.getActualStorageDirectory(mContext), "narration" + mScene.getId() + ".wav");
            if (fileAudio.exists())
                audioPath = fileAudio.getCanonicalPath();
        }
        if ((!fileExport.exists()) || doOverwrite) {
            if (fileExport.exists())
                fileExport.delete();
            fileExport.getParentFile().mkdirs();
            MediaSlideshowExporter mEx = new MediaSlideshowExporter(mContext, mHandler, alMediaIn, fileRenderTmp, audioPath, slideDuration, mMediaDescOut);
            mEx.export();
        }
    }
    deleteRecursive(fileRenderTmp, true);
    return mMediaDescOut;
}
Also used : Message(android.os.Message) AudioClip(org.storymaker.app.model.AudioClip) MediaSlideshowExporter(org.storymaker.app.media.exporter.MediaSlideshowExporter) Media(org.storymaker.app.model.Media) ArrayList(java.util.ArrayList) MediaDesc(org.ffmpeg.android.MediaDesc) MediaVideoExporter(org.storymaker.app.media.exporter.MediaVideoExporter) IOException(java.io.IOException) Date(java.util.Date) SuppressLint(android.annotation.SuppressLint) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) MediaAudioExporter(org.storymaker.app.media.exporter.MediaAudioExporter)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Message (android.os.Message)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 MediaDesc (org.ffmpeg.android.MediaDesc)1 MediaAudioExporter (org.storymaker.app.media.exporter.MediaAudioExporter)1 MediaSlideshowExporter (org.storymaker.app.media.exporter.MediaSlideshowExporter)1 MediaVideoExporter (org.storymaker.app.media.exporter.MediaVideoExporter)1 AudioClip (org.storymaker.app.model.AudioClip)1 Media (org.storymaker.app.model.Media)1