Create a Video file from images using ffmpeg

12,322

I was in a similar need and accomplished the same. There are two ways in which you can do this. I would like to share the simpler one first.

  • Create a temporary folder inside the Android.

  • Copy your images in the new folder

  • First, rename your pictures to follow a numerical sequence. For example, img1.jpg, img2.jpg, img3.jpg,... Then you may run:

  • Run this program programmetcally ffmpeg -f image2 -i img%d.jpg /tmp/a.mpg

To run this programmatically,

Use the following code:

void convertImg_to_vid()
{
    Process chperm;
    try {
        chperm=Runtime.getRuntime().exec("su");
          DataOutputStream os = 
              new DataOutputStream(chperm.getOutputStream());

              os.writeBytes("ffmpeg -f image2 -i img%d.jpg /tmp/a.mpg\n");
              os.flush();

              chperm.waitFor();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Get started with this. I will help you.. All the best

Use the tutorial : http://ffmpeg.org/faq.html Specially gothrough 3.2 section inside the tutorial.

To be able to run the above commands, you should have ffmpeg command in bin directory. The ffmpeg binary should be cross compiled for Android platform...

Share:
12,322
Aashish Bhatnagar
Author by

Aashish Bhatnagar

Android developer for the past 7 years recently started working with React Native.

Updated on June 05, 2022

Comments

  • Aashish Bhatnagar
    Aashish Bhatnagar almost 2 years

    I am able to compile and add ffmpeg to jni folder in my project created Android.mk file now I want to use ffmpeg to create a video file from the images I have stored in my static arraylist

    I have searched alot but couldn't find any tutorial any help is really appreciated.

    • Alexander Kulyakhtin
      Alexander Kulyakhtin about 12 years
      In this link stackoverflow.com/questions/4248316/… the poster says he's able to do so on emulator. You may want to check this. Never did that myself
    • Aashish Bhatnagar
      Aashish Bhatnagar about 12 years
      it's for the iPhone how can I implement it in android
    • Aashish Bhatnagar
      Aashish Bhatnagar about 12 years
      okay I have added the whole ffmpeg-android folder to jni folder added the Android.mk file that you provided now I need to create a C file and add this function to it??
    • Alexander Kulyakhtin
      Alexander Kulyakhtin about 12 years
      I think first you can create something like hello-jni example from ndk to make sure your function gets called. Then you add your ffmpeg code to it, first maybe just av_register_all() to make sure it works. Then you can add that iphone-based code from that link only you have to figure out how to do that for android. I mean, these are only FFMPEG API calls nothing iphone or android-specific. The only tutorial I know is dranger.com/ffmpeg
    • N.Droid
      N.Droid about 12 years
      First of all cross compile ffmpeg for android and create shared object file (.so) from ffmpeg source.
    • Aashish Bhatnagar
      Aashish Bhatnagar about 12 years
      Alex so shall I add another line to Android.mk for my own c file??
    • Abhishek V
      Abhishek V over 10 years
      @Aashish Hi you could able to use ffmpeg fr your project? Can you please suggest some tutorials/links for building ffmpeg for android.Any help is really appreciated.Thanks
  • Aashish Bhatnagar
    Aashish Bhatnagar about 12 years
    so can I use ffmpeg command directly what should I do in JNI folder and Android.mk file I have placed the ffmpeg-android folder as it it in JNI folder i build that from bambuser library
  • Sandeep
    Sandeep about 12 years
    yes like other commands(ls, cd etc..) you can use ffmpeg also by copying the executable inside the bin library. But there are some problems when you want to deploy it in final product. I will help you with that aswell. But initially get started and see if you are able to do this or not..
  • Aashish Bhatnagar
    Aashish Bhatnagar about 12 years
    where would I find "ffmpeg" command and how shuld I place it in the bin folder please guide me thru I am total stranger to ndk and ffmpeg
  • Sandeep
    Sandeep about 12 years
    please google out for ffmpeg code and cross compile it for Android using NDK or any other cross compiler. Once the compilation is finished, result of it would be few libraries and an eeexecutable. The executable copy it in /system/bin using adb and libraries copy in /system/lib.. Once you do that, you can copy the executable from app. Please do some research on building final executable for ffmpeg. It is straight forward. and you will get it from internet.. I exactly dont rememner the source from where did i get the code. Bambuser also i attempted to compile but dont remember which one i used.
  • Sandeep
    Sandeep about 12 years
  • Aashish Bhatnagar
    Aashish Bhatnagar about 12 years
    thanks alot for you help just one last thing I compiled ffmpeg on ubuntu and I am using the resulting directory in windows so what is the executable file like I mean it's syntax and by libraries I hope you mean libavcodec ,etc.. files right ??
  • Sandeep
    Sandeep about 12 years
    I saw you post asking for help on bulding ffmpeg. The make file there will not build the executable. And yes the libraries are libavcodec, etc... there are other places which illustrate you the build of ffmpeg. Those places you need to use... I dont have my code in hand.. I will check it and let you more details tomorrow. But befire that search google frm your end... you will have something like BUILD_EXECUTABLE inside the makefile..
  • Aashish Bhatnagar
    Aashish Bhatnagar about 12 years
    it did create two executable files but /system/bin is read only file system so I am unable to add it there
  • Sandeep
    Sandeep about 12 years
  • Chaitanya Chandurkar
    Chaitanya Chandurkar over 11 years
    Hey.. How can i copy ffmpeg binaries to system/bin from adb?? from adb m not able to ding my ffmpeg binaries !! please help me out !!
  • beni
    beni over 11 years
    @Sandy I've tried to use ffmpeg library with an Android project. But when I've executed the code, the output was: permission denied. Does the device need to be rooted to execute the code? The app should be able to run on most of the devices. Do I need to compile the ffmpeg library, like bambuser code, to execute the code using the Runtime class? Where should I place the compiled library? Do I need to create the Android.mk and Application.mk? If the devices must be rooted I need to compile the library with NDK, which library I should use?
  • Jagdeep Singh
    Jagdeep Singh almost 11 years
    Hi, i read your answer and I am also doing in this way, but I have images in a folder say '/mnt/sdcard/Pic/image%d.jpg' but this gives me an error that no such file or directory found. But if i give '/mnt/sdcard/Pic/image1.jpg' then it works. but how to work with multiple images.??
  • Sandeep
    Sandeep almost 11 years
    Hi Jagdeep. It should work properly. You mmight have done a mistake in naming files some where.Cross check once. As i pointed out please check the FFMPEG faq link. ffmpeg.org/…
  • Ahmad Arslan
    Ahmad Arslan about 10 years
    Can anyone example me how to put the commands? or how commands workks? ffmpeg -f image2 -i img%d.jpg /tmp/a.mpg what is -f? image2?