Why my code doesn't run after my react-native-ffmpeg promise?

535 views Asked by At

After downloading video and audio, I use react-native-ffmpeg to get the final video file. It works. I want to delete the 1.mp4 and 2.mp4 when I get the final video file. The problem is that the code after await getFinalVideo(path, path1, path2, titleVideo) doesn't run. The 1.mp4 and 2.mp4 still exists in the filesystem. How to fix it? Where is my fault?

import React from "react";
import { PermissionsAndroid } from "react-native";
import RNFS from "react-native-fs";
import { LogLevel, RNFFmpeg } from "react-native-ffmpeg";

// url1  video , url2 audio
export const fetchDashFile = async (url1, url2, title) => {
  let options1 = {
    method: "GET",
    headers: {},
  };
  let options2 = {
    method: "GET",
    headers: {},
  };

  let path = RNFS.DownloadDirectoryPath + "/";
  let path1 = path + "1" + ".mp4";
  let path2 = path + "2" + ".mp4";

  const beginVideo = (res) => {
    console.log("start download video file");
  };
  const beginAudio = (res) => {
    console.log("start download audio file");
  };

  const progressVideo = (res) => {
    let percentage = ((100 * res.bytesWritten) / res.contentLength) | 0;
    console.log(percentage);
  };

  const progressAudio = (res) => {};

  let DownloadFileOptions1 = {
    fromUrl: url1,
    toFile: path1,
    headers: options1.headers,
    background: true,
    connectionTimeout: 50,
    progressInterval: 1,
    progressDivider: 10,
    begin: beginVideo,
    progress: progressVideo,
  };
  let DownloadFileOptions2 = {
    fromUrl: url2,
    toFile: path2,
    headers: options2.headers,
    background: true,
    connectionTimeout: 50,
    progressInterval: 1,
    progressDivider: 10,
    begin: beginAudio,
    progress: progressAudio,
  };

  try {
    await Promise.all([
      RNFS.downloadFile(DownloadFileOptions1).promise,
      RNFS.downloadFile(DownloadFileOptions2).promise,
    ]);

    let titleVideo = Date.now().toString();
    await getFinalVideo(path, path1, path2, titleVideo);

    if (RNFS.exists(path1)) {
      console.log("delete 1");
      await RNFS.unlink(path1);
    }
    if (RNFS.exists(path2)) {
      console.log("delete 2");
      await RNFS.unlink(path2);
    }
  } catch (e) {
    alert("error,please try again");
  }
};

function getFinalVideo(path, path1, path2, name) {
  let command =
    "-i " + path1 + " -i " + path2 + " -c copy " + path + name + ".mp4";
  return new Promise((resovle, reject) => {
    RNFFmpeg.executeAsync(command, (completedExecution) => {
      if (completedExecution.returnCode === 0) {
        console.log("FFmpeg process completed successfully");
      } else {
        console.log(
          `FFmpeg process failed with rc=${completedExecution.returnCode}.`
        );
      }
    })
      .then((executionId) => {
        console.log(executionId);

        Promise.resolve("success");
      })
      .catch((e) => {
        console.log("wrong happened when doing ffmpeg" + e);
        Promise.reject(e);
      });
  });
}
1

There are 1 answers

0
Hidden On

I'm so stupid. Before Deleting files, it need to wait for finishing ffmpeg work! I misunderstood something about Promise. So here is right code.

...
  let titleVideo = Date.now().toString()
    await getFinalVideo(path, path1, path2, titleVideo)
...


async function getFinalVideo(path, path1, path2, name) {
  let command = '-i ' + path1 + ' -i ' + path2 + ' -c copy ' + path + name + '.mp4'
  await RNFFmpeg.executeAsync(command, completedExecution => {
    if (completedExecution.returnCode === 0) {
      console.log("FFmpeg process completed successfully");

    } else {
      console.log(`FFmpeg process failed with rc=${completedExecution.returnCode}.`)

    }
  })
    .then(executionId => {
      console.log(executionId)
     // RNFFmpeg.cancelExecution(executionId)



    })
    .catch(e => {
      console.log('wrong happened when doing ffmpeg' + e)
    })