1 Official reference and project address used

Ffmpeg-kit Warehouse address ffMPEG-kit-test warehouse address FFMPEG official reference document FFMPEG-kit-Android Usage Reference Use ffmpeg to compress videos

2 FFmpegKit overview

FFmpegKit is a script that encapsulates the FFmpeg library

FFmpegKit is a wrapper library that runs the FFmpeg/FFprobe command in an application

Supports Android, iOS, macOS and tvOS

Eight pre-built binaries are available on FFmpeg Github, Maven Central, and CocoaPods based on V4.4-dev with optional systems and external libraries. Under license LGPL 3.0, or GPL V3.0 if a GPL-licensed library is enabled

2 Introduction of Android

2.1 Errors to be encountered

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDebugNativeLibs'.
   > More than one file was found with OS independent path 'lib/armeabi-v7a/libc++_shared.so'. If you are using jniLibs and CMake IMPORTED targets, see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmakeError: the local project references the same.so file as the third party library, or two third party libraries introduce the same.so file, causing compilation not to know which is the main fileCopy the code

2.2 How to Handle the fault

  • Refer to www.freesion.com/article/502…
  • Follow the tips given to start an RN project

  • Add this line of code to the Android {} build.gradle file in the Android Studio Modify Android /app directory. And I’m gonna hit Sync
 packagingOptions {
        pickFirst  'lib/x86/libc++_shared.so'
        pickFirst  'lib/arm64-v8a/libc++_shared.so'
        pickFirst  'lib/x86_64/libc++_shared.so'
        pickFirst  'lib/armeabi-v7a/libc++_shared.so'
    }
Copy the code

3 React Native Sends the FFmPE command to the Android native layer

3.1 Androidmanifest.xml registration permissions

  • Permission to register

3.2 the use ofreact-native-image-crop-pickerGet the video path, generate ffMPE command and pass it to andorid native layer for execution

import React, { Component } from 'react';
import { View, Text, Button } from 'react-native';
import ImagePicker from 'react-native-image-crop-picker';
import ToastExample from '.. /.. /native/ToastExample';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      path: ' '
    };
  }

  /** * Select video */ from gallery
  pickVideo = () = > {
    ImagePicker.openPicker({
      mediaType: 'video'
    })
      .then((image) = > {
        console.log(image);
        this.setState({ path: image.path });
      })
      .catch((error) = > {
        console.log(error);
      });
  };

  toast = () = > {
    const command = `-i The ${this.state.path }-r 20 /sdcard/$MuMu Shared folder /1111.mp4 '
    console.log(command)
    ToastExample.show(command);
  };

  render() {
    return (
      <View style={{ flex: 1}} >
        <Text> videoPick </Text>
        <Button title="Select video" onPress={this.pickVideo} />
        <Button title="toast" onPress={this.toast} />
      </View>); }}Copy the code

3.3 Android native layer code implementation

    @ReactMethod
    public void show(String message) {
        Log.d(TAG, message);
// Toast.makeText(getReactApplicationContext(), message, duration).show();

        FFmpegSession session = FFmpegKit.execute(message);
        if (ReturnCode.isSuccess(session.getReturnCode())) {

            // SUCCESS
            Log.d(TAG, "show: SUCCESS"+ session.getOutput());

        } else if (ReturnCode.isCancel(session.getReturnCode())) {
            // CANCEL
            Log.d(TAG, "show: CANCEL");

        } else {
            // FAILURE
            Log.d(TAG, String.format("Command failed with state %s and rc %s.%s", session.getState(), session.getReturnCode(), session.getFailStackTrace())); }}Copy the code

4 Ffmpeg Common commands

4.1 Video Compression Process

  • Select video: Android MP4
  • After the compressed video is uploaded, it is compressed and uploaded (uploaded), and the compressed file is deleted

4.2 Video clipping first

ffmpeg -ss 00:00:00 -i video.mp4 -vcodec copy -acodec copy -t 00:00:31 output1.mp4

4.3 after compression

Path − R20 /sdcard/{this.state.path} -r 20/sdcard/ this.state.path− R20 /sdcard/MuMu shared folder /1111.mp4

4.4 Audio Extraction (Into mobile Phone)

Ffmpeg-i if only as first seen. Flv-vn r.mp3 #-vn 解析 video no 解析Copy the code

4.5 compressed mp3

Ffmpeg -i input.mp3 -ab 128 output.mp3

Differences between H264 and H265

The H265 is even better in motion and frame loss situations