tanersener / react-native-ffmpeg

FFmpeg for react-native. Not maintained anymore. Superseded by FFmpegKit.
GNU General Public License v3.0
401 stars 52 forks source link

check if command is executed #125

Closed issamabdelkrim closed 4 years ago

issamabdelkrim commented 4 years ago

Hi everyone How i can check if there is a command is executed in real time and get status if there is finished ? Because here i have a chat application with react native and user can send a video and before the video terminate compress he send another so it will call the method of compress another time without terminate the compress of the first ? here is my code : for (var i = 0; i < media.length; i++) { // console.log("send :", media[i].uri); this.setState({ mediaUploaded: true });

        let name;

        if (media[i].image) {
          type = "image/jpeg";
          name = "photo.jpg";
          data.append("files[" + i + "]", {
            uri:
              Platform.OS === "android"
                ? media[i].uri
                : media[i].uri.replace("file://", ""), // your file path string
            name: name,
            type: type
          });
        } else if (media[i].video) {
          let output;
          //setTimeout(this.compressionVideo(),2000);
          if (media[i].camera) {
            await this.convertVideo(i.toString(),
              Platform.OS === "android"
                ? media[i].path
                : media[i].uri.replace("file://", "")
            );
          } else
            output = await this.convertVideo(i.toString(),
              Platform.OS === "android"
                ? media[i].uri
                : media[i].uri.replace("file://", "")
            );
          // console.log(output);
          type = "video/mp4";
          name = "video.mp4";
          let file = "";

          if (Platform.OS == "ios")
            file = `${RNFetchBlob.fs.dirs.DocumentDir}/convert${i.toString()}.mp4`;
          else file = `file://${RNFetchBlob.fs.dirs.PictureDir}/convert${i.toString()}.mp4`;
          console.log(file);
          data.append("files[" + i + "]", {
            uri: file, // your file path string
            name: name,
            type: type
          });
        }
      }

async convertVideo(i,uri) {

  console.log(uri);
let index=i;
RNFetchBlob.fs
  .stat(uri)
  .then(stats => {
    this.setState({ size: stats.size });
  })
  .catch(err => {
    console.log(err);
  });
let outputpath = "";

this.setState({videoConvert:true})

if (Platform.OS == "ios")
  outputpath = `${RNFetchBlob.fs.dirs.DocumentDir}/convert${i}.mp4`;
else outputpath = `${RNFetchBlob.fs.dirs.PictureDir}/convert${i}.mp4`;
const fs = RNFetchBlob.fs
if (Platform.OS == "ios"){
  await  RNFetchBlob.fs.writeFile(outputpath, '', 'uri')
  .then((success) => {
    console.log('FILE WRITTEN!');
  })
}

if (Platform.OS === "android") {
  await this.requestPermission();
}

RNFFmpeg.resetStatistics();

RNFFmpeg.enableStatisticsCallback(this.statisticsCallback);
const path=this.addQuotes(uri);
let command= `-i  ${path}   -vf  scale=-2:-2:force_original_aspect_ratio=decrease,setsar=1  -vcodec libx264 -y  ${outputpath}`;
console.log(uri, " command : ",command);
await RNFFmpeg.execute(

command ).then(result => { let messagesChat = [...this.state.messagesChat]; for (var i = 0; i < messagesChat.length; i++) { let item = messagesChat[i]; this.setState({videoConvert:false}) if (item && item.destination === "web") { let media = item.media[0].media; if(media[index]) media[index].compression = false;

    }

    messagesChat[i] = {
      ...item
    };
  }

  this.setState({ messagesChat: messagesChat, compressionValue: 0 });
});

}

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.