• Stars
    star
    209
  • Rank 188,325 (Top 4 %)
  • Language
    Kotlin
  • License
    GNU General Publi...
  • Created almost 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Examples of using FFMpeg library on Android with Kotlin

Kotlin FFMpeg Tools Android Arsenal

Examples of using FFMpeg library on Android with Kotlin

For Video, Audio and Image/GIF operations.

App is pre loaded with audio, video, images, font resources which are useful for experimenting with FFmpeg library. You can add your resources as well, but keep extensions and make sure naming conventions are same as used in this project to avoid any I/OExceptions.

Each example will preview result in a dialog. Files are saved in local storage directory and can be directly accessed from there.

Image1 Image2

FFMpeg Examples (Video):

  1. Split a video in equal segments

    VideoSplitter.with(context!!)
                     .setFile(video) //Video File
                     .setOutputPath("PATH_TO_OUTPUT_VIDEO")
                     .setSegmentTime("00:00:15") //Split into 15 seconds segment
                     .setOutputFileName("splittedVideo")
                     .setCallback(this@MainActivity)
                     .split()
    
  2. Trim a video with specified timestamps

     VideoTrimmer.with(context!!)
                     .setFile(video) //Video File
                     .setStartTime("00:00:15") // Start from 15 seconds
                     .setEndTime("00:00:25") //End on 25 seconds
                     .setOutputPath("PATH_TO_OUTPUT_VIDEO")
                     .setOutputFileName("trimmed_" + System.currentTimeMillis() + ".mp4")
                     .setCallback(this@MainActivity)
                     .trim()
    
  3. Make a MP4 movie with provided images & audio in mp3

      MovieMaker.with(context!!)
                             .setAudio(audio2) //Audio File
                             .setOutputPath("PATH_TO_OUTPUT_VIDEO")
                             .setOutputFileName("movie_" + System.currentTimeMillis() + ".mp4")
                             .setCallback(this@MainActivity)
                             .convert()
    
  4. Resize a video in specified dimensions

     VideoResizer.with(context!!)
                             .setFile(video2) //Video File
                             .setSize("320:480") //320 X 480
                             .setOutputPath("PATH_TO_OUTPUT_VIDEO")
                             .setOutputFileName("resized_" + System.currentTimeMillis() + ".mp4")
                             .setCallback(this@MainActivity)
                             .resize()
    
  5. Add Text overlay on video with specified text attributes

     TextOnVideo.with(context!!)
                             .setFile(video2) //Video File
                             .setOutputPath("PATH_TO_OUTPUT_VIDEO")
                             .setOutputFileName("textOnVideo_" + System.currentTimeMillis() + ".mp4")
                             .setFont(font) //Font .ttf of text
                             .setText("Text Displayed on Video!!") //Text to be displayed
                             .setColor("#50b90e") //Color of Text
                             .setSize("34") //Size of text
                             .addBorder(true) //This will add background with border on text
                             .setPosition(TextOnVideo.POSITION_CENTER_BOTTOM) //Can be selected
                             .setCallback(this@MainActivity)
                             .draw()
    
  6. Merge an audio over video file

     AudioVideoMerger.with(context!!)
                             .setAudioFile(audio) //Audio File
                             .setVideoFile(video2) //Video File
                             .setOutputPath("PATH_TO_OUTPUT_VIDEO")
                             .setOutputFileName("merged_" + System.currentTimeMillis() + ".mp4")
                             .setCallback(this@MainActivity)
                             .merge()
    
  7. Merge/Concatenate multiple video files into single video file

     val videoList = arrayListOf<File>(videoSmall1, video, video2)
     
     VideoMerger.with(context!!)
                             .setVideoFiles(videoList) //Video Files list
                             .setOutputPath("PATH_TO_OUTPUT_VIDEO")
                             .setOutputFileName("merged_" + System.currentTimeMillis() + ".mp4")
                             .setCallback(this@MainActivity)
                             .merge()
    

FFMpeg Examples (Audio):

  1. Trim a audio with specified timestamps

     AudioTrimmer.with(context!!)
                             .setFile(audio2) //Audio File
                             .setStartTime("00:00:05") //Start at 5 seconds
                             .setEndTime("00:00:10") //End at 10 seconds
                             .setOutputPath("PATH_TO_OUTPUT_AUDIO")
                             .setOutputFileName("trimmed_" + System.currentTimeMillis() + ".mp3")
                             .setCallback(this@MainActivity)
                             .trim()
    
  2. Extract audio file from video file

     AudioExtractor.with(context!!)
                             .setFile(video2) //Video File
                             .setOutputPath("PATH_TO_OUTPUT_AUDIO")
                             .setOutputFileName("audio_" + System.currentTimeMillis() + ".mp3")
                             .setCallback(this@MainActivity)
                             .extract()
    
  3. Merge/Overlay two Audio files

     AudioMerger.with(context!!)
                            .setFile1(audio2)
                            .setFile2(audio3)
                            .setOutputPath(Utils.outputPath + "audio")
                            .setOutputFileName("merged_" + System.currentTimeMillis() + ".mp3")
                            .setCallback(this@MainActivity)
                            .merge()
    

FFMpeg Examples (Image):

  1. Convert video to GIF

     VideoToGIF.with(context!!)
                             .setFile(video) //Video File
                             .setOutputPath("PATH_TO_OUTPUT_IMAGE")
                             .setOutputFileName("myGif_" + System.currentTimeMillis() + ".gif")
                             .setDuration("5") //Gif duration
                             .setScale("500") //Size of GIF
                             .setFPS("10") //Frame rate of GIF
                             .setCallback(this@MainActivity)
                             .create()
    
  2. Convert video to images every 'n' time

     VideoToImages.with(context!!)
                             .setFile(video2) //Video File
                             .setOutputPath("PATH_TO_OUTPUT_IMAGE")
                             .setOutputFileName("images")
                             .setInterval("0.25") // Extract image every quarter of second (0.25)
                             .setCallback(this@MainActivity)
                             .extract()
    

CallBacks:

You can implement callbacks of all tools using: FFMpegCallback. Just implement this in your Activity or Fragment.

It supports following callbacks:

        override fun onProgress(progress: String) {
            Log.i(TAG, "Running: $progress")
        }
    
        override fun onSuccess(convertedFile: File, type: String) {
            Toast.makeText(this, "Done!", Toast.LENGTH_SHORT).show()
        }
    
        override fun onFailure(error: Exception) {
            error.printStackTrace()
        }
    
        override fun onFinish() {
            //TODO Do something here
        }
    
        override fun onNotAvailable(error: Exception) {
            Toast.makeText(this, "Error: ${error.message}", Toast.LENGTH_SHORT).show()
        }

Stop/Kill ongoing process:

        fun stopRunningProcess() {
                FFmpeg.getInstance(this).killRunningProcesses()
        }

Check if any process is running:

        fun isRunning(): Boolean {
                return FFmpeg.getInstance(this).isFFmpegCommandRunning
        }

Permissions:

Following permissions must be added to avoid IO exceptions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

TODO:

  • Add preview for 'Extarct Images from Video'

Acknowledgments:

MIT License

Copyright (c) 2018 Muhammad Umair Adil

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Support

Buy a coffe to Umair Adil, creator of this plugin.

Buy me a coffee

More Repositories

1

tensorflow_lite_flutter

A flutter project for demonstarting usage of TensorFlow Lite model created with teachablemachine.
Dart
88
star
2

flutter_logs

An extensive logging framework developed for flutter apps.
Kotlin
40
star
3

simple_beacons_flutter

A flutter plugin project to range & monitor iBeacons.
Kotlin
35
star
4

AndroidArchiTemplate

A sample project to demonstrate Android new Architecture Components with Kotlin & Dagger2.
Kotlin
26
star
5

pixabay_flutter_demo

A showcase app for displaying image lists, developed on Flutter.
Dart
26
star
6

mp4parser

Android App to Append Mp4 Videos or Merge Audio files with video files on your Android device.
Java
17
star
7

RxLogs

An Android & Kotlin Reactive Advanced Logging Framework.
Kotlin
12
star
8

AndroidJetpackTemplate

This is a template project with Android JetPack & AndroidX implementation.
Kotlin
11
star
9

background_stt

A flutter plugin to run always-on speech to text service in the background.
Kotlin
10
star
10

SmartIndoor-PositioningAndroid

A simple app to scan nearby Wi-Fi access points and locate position of the device indoors.
Kotlin
5
star
11

mapbox_navigation

A Flutter plugin for MapBox's Navigation SDK, NavigationView & Navigation services.
Kotlin
5
star
12

trace_19_covid

Flutter app for Covid-19 survey app.
Dart
4
star
13

RxImageUtils

A project to demonstrate image file opertaions like encryption & decryption.
Kotlin
4
star
14

KotlinAlgorithms

Collection of solutions for algorithm problems from Codility and other sources in Kotlin.
Kotlin
3
star
15

CVMaker

A simple app to create a single Page CV and export it as PDF.
Kotlin
3
star
16

background_tts_stt

A flutter project to run speech recognition service in background.
Java
3
star
17

flutter_app_testing

A flutter app to demonstrate unit, widget & integration testings.
Dart
1
star
18

SmartLoader

Smartloader automatically downloads & caches images in background. It's light weight. It cancels all download requests once activty/fragment is destroyed. It manages all needs for loading images into ImageView. It can also support large list where multiple images are requested at once.
Kotlin
1
star
19

flutter_in_urdu

This project is part of Youtube course: Flutter in Urdu.
Dart
1
star