how to cut or trim audio song in android programmatically? - android

I want to cut or trim audio song in android programmatically. i have found FFMPEG solution but i am not getting what is the step behind to cut audio song and if any other way please help me.
most people give me this type answer
ffmpeg -t 30 -i inputfile.mp3 -acodec copy outputfile.mp3
what is this and how to use in android code to cut audio?
Please help me.
Thank You

Basically follow the steps described here: http://writingminds.github.io/ffmpeg-android-java/
1.) You have to include the ffmpeg Library into your project!
put this in your build.gradle File:
dependencies {compile 'com.writingminds:FFmpegAndroid:0.3.2'}
2.) before using the Lib, copy the binary file from assets to the device:
FFmpeg ffmpeg = FFmpeg.getInstance(context);
ffmpeg.loadBinary(new LoadBinaryResponseHandler() {...}
3.) when this is finished you can start executing commands and listen for the results like this:
ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {...}
where cmd is the array of arguments:
String[] command1 = new String[8];
command1[0]="-t";
command1[0]="30";
...
BUT: actually I found that the ffmpeg version for android does not work as expected for cutting or trimming audio files! (I got errors for commands that do work on linux commandline...) Hope you will figure it out.

There's a simple hack:
figure out bytes per second or millisecond -- file.length()/duration
get the start and end position where you want to cut audio.
read file as bytes and save the portion between startPos and EndPos in separate file.

Related

How to convert images to video with ffmpeg api not commands?

I'm trying to convert images(as jpg) to video(as mp4),
but looking up at the ffmpeg docs, I just found command lines as follow
ffmpeg -f image2 -i img%d.jpg /tmp/a.mpg
Does anyone know the api with c or c++ code? Thanks.
pseudo code:
Open output file;
Open output code;
while(input files remain) {
Open input file;
Decode input frame;
Give decoded frame a timestamp;
Encode frame;
Write frame to output file;
}
Flush encode;
Close output file;
Do the steps in order. Im sure you'll have question as you implement this. When you have a question, Open a new topic on stack overflow with the code you have so far with a specific problem, and what you have already tried.

How to use file *.acv to video in android

I have an application Andoroid . I want to see more effects to video using photoshop file and preview it .acv . I have problems with the preview . Who has the solution to the problem by applying filters * .acv for video , thanks .
i used this
library and follwing is the command
String[]
complexCommand={"ffmpeg","-y","-i","file path of input video ","-strict","experimental","-vf","curves=psfile=filter acv file path ","-b","2097k","-vcodec","mpeg4","-ab","48000","-ac","2","-ar","22050","file path of the output video"}
for example
String[]
complexCommand={"ffmpeg","-y","-i","/storage/emulated/0/vk2/in.mp4","-strict","experimental","-vf","curves=psfile=/storage/emulated/0/videokit/sepia.acv","-b","2097k","-vcodec","mpeg4","-ab","48000","-ac","2","-ar","22050","/storage/emulated/0/videokit/out.mp4"}
given link is a forum you may ask your questions regarding ffmepeg there

FFMPEG Images to video - 'file not found' error in command - I'm sure it's something obvious

I'm running this command on my Android phone with an FFmpeg library. It works great for combining videos into one resultant video, but not with images. Here's the command:
-framerate 4 -start_number 0 -i '/storage/emulated/0/Pictures/phototest/%3d.jpg' -c:v libx264 -r 30 -pix_fmt yuv420p /storage/emulated/0/Pictures/phototest/result.mp4
The error is:
'/storage/emulated/0/Pictures/phototest/%3d.jpg': No such file or directory
The images are just consecutively named, e.g. 1.jpg, 2.jpg and so on, and they're definitely there - though I think FFMPEG is taking the image file name literally rather than interpreting it. I'm missing something obvious here, I'm sure of it. I looked round StackOverflow and the whole '%3d' wildcard is supposed to work! Any ideas?
EDIT: Running the command with just the first image (1.jpg) works fine. Definitely an issue with the wildcard. In addition, running the command without the image path in quotes doesn't seem to work either.
EDIT 2: Adding 'img' in front of every file so that it it's 'img1.jpg', 'img2.jpg' and so on doesn't work either. Is there a way to pass multiple images separated by '|' like the concat command?
If your filenames are 1.jpg, 2.jpg, 3.jpg, ...
then you have to use
'/storage/emulated/0/Pictures/phototest/%d.jpg'
If your filenames are img1.jpg, img2.jpg, img3.jpg, ...
then you have to use
'/storage/emulated/0/Pictures/phototest/img%d.jpg'
If your filenames are img01.jpg, img02.jpg, img03.jpg, ...
then you have to use
'/storage/emulated/0/Pictures/phototest/img%02d.jpg'
If your filenames are img001.jpg, img002.jpg, img003.jpg, ...
then you have to use
'/storage/emulated/0/Pictures/phototest/img%03d.jpg'
For more details refer this https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence
I figured it out. %3d actually is equal to 01, 02, 03, etc. So taking the 3 away for just '%d' worked fine.

extract all video frames in Android

I recorded a Video for limited time. Now i want to fetch all frames of video. I am using the below code and by using it i am able to get frames but i am not getting all video frames. 3 to 4 frames are repeated then i got a different frame. But as we all know we can get 25- 30 frames in a second to display smooth video. How to get all frames.
for (int i = 0; i < 30; i++) {
Bitmap bArray = mediaMetadataRetriever.getFrameAtTime(
1000000 * i,
MediaMetadataRetriever.OPTION_CLOSEST);
savebitmap(bArray, 33333 * i);
}
I don't want to use NDK. I got this link don't know what should be the value for "argb8888". I am getting error here. Can anyone explain how to do it.
Getting frames from Video Image in Android
I faced the same problem before and the Android's MediaMetadataRetriever seems not appropriated for this task since it doesn't have a good precision.
I used a library called "FFmpegMediaMetadataRetriever" in android studio:
Add this line in your build.graddle under module app:
compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'
Rebuild your project.
Use the FFmpegMediaMetadataRetriever class to grab frames with higher
precision:
FFmpegMediaMetadataRetriever med = new FFmpegMediaMetadataRetriever();
med.setDataSource("your data source");
and in your loop you can grab frame using:
Bitmap bmp = med.getFrameAtTime(i*1000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
To get image frames from video we can use ffmpeg.For integrating FFmpeg in android we can use precompiled libraries like ffmpeg-android.
To extract image frames from a video we can use below command
String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-an",
"-r", "1/2", "-ss", "" + startMs / 1000, "-t", "" + (endMs - startMs)
/ 1000, outputFileAbsolutePath};
Here,
-y
Overwrite output files
-i
ffmpeg reads from an arbitrary number of input “files” specified by the -i option
-an
Disable audio recording.
-r
Set frame rate
-ss
seeks to position
-t
limit the duration of data read from the input file
Here in place of inputFileAbsolutePath you have to specify the absolute path of video file from which you want to extract images.
For complete code check out this on my repository .Inside extractImagesVideo() method I am running command for extracting images from video.
For complete tutorial regarding integration of ffmpeg library and using ffmpeg commands to edit videos, check out this post which I have written on my blog.
You need to do :
Decode the video.
Present the decoded images at least as fast as 24 images / second. I
suppose you can skip this step.
Save the decoded images.
It appears that decoding the video would be the most challenging step. People and companies have spent years developing codecs (encoder / decoder) for various video formats.
Use this library JMF for FFMPEG.

CvCaptureFromAVI problems - OpenCV Android

I need to capture frame by frame from a video stored in my sd card of the Android device (in this case my emulator). I am using Android and OpenCV through NDK. I pushed manually the file "SinglePerson.avi" inside the sdcard through file explorer of DDBS (eclipse) and I used the code below to read the file:
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_Sample4Mixed_VideoProcessing(JNIEnv*, jobject)
{
LOGI("INSIDE VideoProcessing ");
CvCapture* capture = cvCaptureFromAVI("/mnt/sdcard/SinglePerson.avi");
IplImage* img = 0;
if(!cvGrabFrame(capture)){ // capture a frame
LOGI("Inside the if");
printf("Could not grab a frame\n\7");
exit(0);
}
img=cvRetrieveFrame(capture);// retrieve the captured frame
cvReleaseCapture(&capture);
}
The problem is that cvGrabFrame(capture) results always false.
Any suggestion to correctly open the video and grab the frames?
Thanks in advance
Some versions of OpenCV (in package opencv2) build without video support. If it is your case you have to enable "-D WITH_FFMPEG=ON" in pkg's Makefile and recompile.
Look at "Displaying AVI Video using OpenCV" tutorial:
"You may need to ensure that ffmpeg has been successfully installed in order to allow video encoding and video decoding in different formats. Not having the ffmpeg functionality may cause problems when trying to run this simple example and produce a compilation errors".
Also check path in cvCaptureFromAVI for correctness.
Hope this will help!
The behavior you are observing is probably due to cvCaptureFromAVI() failing. You need to start coding safely and check the return of the calls you make:
CvCapture* capture = cvCaptureFromAVI("/mnt/sdcard/SinglePerson.avi");
if (!capture)
{
printf("!!! Failed to open video\n\7");
exit(0);
}
This function usually fails for 2 reasons:
When it's unable to access the file (due to wrong filesystem permissions);
Missing codecs on the system (or the video format is not supported by OpenCV).
If you are new to OpenCV, I suggest you test your OpenCV code on a desktop (PC) first.

Categories

Resources