I have a job that is rotating and trimming video files.
I do trimming the video but couldn't rotate it.
I use following code snippet to rotate but the result video is the same with the source video .Also there isn't any error messeage.
videoPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download/cvbenim/islenecek.mp4";
try {
String rotatedPath = videoPath.replace(".mp4", "cvbenim_is_ilanı_rotated.mp4");
Movie result = MovieCreator.build(videoPath);
File file = new File(rotatedPath);
if (file.exists()) {
file.delete();
}
Container out = new DefaultMp4Builder().build(result);
MovieHeaderBox mvhd = Path.getPath(out, "moov/mvhd");
mvhd.setMatrix(Matrix.ROTATE_90);
out.writeContainer(new FileOutputStream(rotatedPath).getChannel());
playVideoFromPath(rotatedPath);
} catch (Exception e) {
e.printStackTrace();
}
I appriciate any help.
You don't state how you present your video in playVideoFromPath but as https://stackoverflow.com/a/17395134/3233251
says.
When you playback the video on Android with the help of the VideoView you might notice that the matrix is not taken into account. I'm not entirely sure if this is done on purpose or not but the workaround is to use a TextureView that applies the transformation.
So you should try to use the TextureView as recommended if you are not already doing so.
Related
I am recording video with audio using MediaRecorder, however when you take pictures with Camera you can save the image into a Bitmap and display it before saving, I want to do the same with the video - like Snapchat does, is there a way? I don't want to save the video, display it, and then have the option to delete or keep it.
I am using this code in order to record videos:
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SelfieLightCamera/";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
String myFile = path + "Video_" + System.currentTimeMillis() + ".mp4";
mediaRecorder = new MediaRecorder();
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setOutputFile(myFile);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
e.printStackTrace();
} catch (IOException e) {
releaseMediaRecorder();
e.printStackTrace();
}
mediaRecorder.start();
The cool thing about Android apps is that you can extract the APK from your phone and decompile the package to see the actual Java code. Although it's usually obfuscated (as is the case with Snapchat), you can still get quite a good view into the inner workings of the application.
When looking into how Snapchat does it, I found out that they also use MediaRecorder just like you but save the file into internal storage. Internal storage means that the file is only accessible by the app that holds it.
Here's a few lines of code that gives you a general idea of Snapchat does behind the scenes:
mediaRecorder.setCamera(ay$b.b());
mediaRecorder.setAudioSource(5);
if (z) {
mediaRecorder.setVideoSource(2);
} else {
mediaRecorder.setVideoSource(1);
}
mediaRecorder.setProfile(camcorderProfile);
mediaRecorder.setVideoSize(i, i2);
mediaRecorder.setMaxFileSize(bm.a());
mediaRecorder.setVideoEncodingBitRate(bm.a(camcorderProfile));
// Removed some code here…
mediaRecorder.setOrientationHint(this.f);
mediaRecorder.setMaxDuration(HttpService.DEFAULT_READ_TIMEOUT);
mediaRecorder.setOutputFile(this.a.toString());
if (this.c != null) {
mediaRecorder.setPreviewDisplay(this.c);
What you should do is record the video normally and save it to the internal storage. If you want to "keep" it, you just move it to the external storage where it's publicly available. Discarding it means that you just delete it from the internal storage.
I have created functionality to record video in my app.
When I play a song, that song is recorded with video and a video file is created, similar to a dubshmash application.
Now the problem that I am facing is that other voices such as near by sounds also get recorded. The song file is recorded in the video record screen and I play the song when video recording activity launches.
How can I have my application record only song with video?
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
Is there any solution in audio source set as a speaker , because song sound going through a speaker? if is it another possible way please reply me.
You can record video without audio and merge audio later on using mp4 parser like this:
/*
* #param videoFile path to video file
* #param audioFile path to audiofile
*/
public String mux(String videoFile, String audioFile) {
Movie video = null;
try {
video = new MovieCreator().build(videoFile);
} catch (RuntimeException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
Movie audio = null;
try {
audio = new MovieCreator().build(audioFile);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
int size = audio.getTracks().size();
Track audioTrack = audio.getTracks().get((size - 1));
video.addTrack(audioTrack);
Container out = new DefaultMp4Builder().build(video);
File myDirectory = new File(Environment.getExternalStorageDirectory(), "/Folder Name");
if (!myDirectory.exists()) {
myDirectory.mkdirs();
}
filePath = myDirectory + "/video" + System.currentTimeMillis() + ".mp4";
try {
RandomAccessFile ram = new RandomAccessFile(String.format(filePath), "rw");
FileChannel fc = ram.getChannel();
out.writeContainer(fc);
ram.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return filePath;
}
In build.gradle add following dependency
compile 'com.googlecode.mp4parser:isoparser:1.0.5.4'
If you want to working with video then you have to use FFMPEG library
That can be you can work with Video.
That for i have already give answer to How to use ffmpeg in android studio? see this LINK. Go step by step and import in your project
You can use a MediaRecorder without calling setAudio* on it.
remove this line
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
see this link
There is currently no way to directly record android output without "background noise".
Note that this is a security concern to restrict access to other apps audio output, therefore it is very unlikely that it could be achieved directly.
See this answer
I have created functionality to record video in my app.
When I play a song, that song is recorded with video and a video file is created, similar to a dubshmash application.
Now the problem that I am facing is that other voices such as near by sounds also get recorded. The song file is recorded in the video record screen and I play the song when video recording activity launches.
How can I have my application record only song with video?
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
Is there any solution in audio source set as a speaker , because song sound going through a speaker? if is it another possible way please reply me.
You can record video without audio and merge audio later on using mp4 parser like this:
/*
* #param videoFile path to video file
* #param audioFile path to audiofile
*/
public String mux(String videoFile, String audioFile) {
Movie video = null;
try {
video = new MovieCreator().build(videoFile);
} catch (RuntimeException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
Movie audio = null;
try {
audio = new MovieCreator().build(audioFile);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
int size = audio.getTracks().size();
Track audioTrack = audio.getTracks().get((size - 1));
video.addTrack(audioTrack);
Container out = new DefaultMp4Builder().build(video);
File myDirectory = new File(Environment.getExternalStorageDirectory(), "/Folder Name");
if (!myDirectory.exists()) {
myDirectory.mkdirs();
}
filePath = myDirectory + "/video" + System.currentTimeMillis() + ".mp4";
try {
RandomAccessFile ram = new RandomAccessFile(String.format(filePath), "rw");
FileChannel fc = ram.getChannel();
out.writeContainer(fc);
ram.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return filePath;
}
In build.gradle add following dependency
compile 'com.googlecode.mp4parser:isoparser:1.0.5.4'
If you want to working with video then you have to use FFMPEG library
That can be you can work with Video.
That for i have already give answer to How to use ffmpeg in android studio? see this LINK. Go step by step and import in your project
You can use a MediaRecorder without calling setAudio* on it.
remove this line
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
see this link
There is currently no way to directly record android output without "background noise".
Note that this is a security concern to restrict access to other apps audio output, therefore it is very unlikely that it could be achieved directly.
See this answer
My application captures video footage and saves it as .mp4 file. I would like to extract one frame from this video and also save it to file. Since I haven't found nothing better, I've decided to use MediaMetadataRetriever.getFrameAtTime() for that. It happens inside the class that inherits from AsyncTask. Here is how my code looks like my doInBackground():
Bitmap bitmap1 = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(src);
bitmap1 = retriever.getFrameAtTime(timeUs, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
if (Utils.saveBitmap(bitmap1, dst)) {
Log.d(TAG, "doInBackground Image export OK");
} else {
Log.d(TAG, "doInBackground Image export FAILED");
}
} catch (RuntimeException ex) {
Log.d(TAG, "doInBackground Image export FAILED");
} finally {
retriever.release();
if (bitmap1 != null) {
bitmap1.recycle();
}
}
And the saveBitmap() method:
File file = new File(filepath);
boolean result;
try {
result = file.createNewFile();
if (!result) {
return false;
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
Now, the problem is that the quality of the exported image is noticeably worse then the video quality. I don't think that this should happen with PNG output format. You can see the difference below:
The first image was extracted from the video using ffmpeg on my desktop. The second one was extracted using the code above on Samsung Galaxy S6. The result looks pretty much the same on every Android device I was using.
Can someone tell how can I improve the quality of the exported picture?
I found other solution for the issue. You can use bigflake's example to build mechanism for extracting video frame. The one thing you will have to add is seeking mechanism. This works well, keeps the exact quality and does not require any third-party libraries. Only downside I've noticed so far is that it will result in longer execution time than the original idea.
hi can anyone tell me how to take snapshot of a video being played in the videoview. the snapshot gives blank image, or any method to grab the current frame of video, i want to pick the current frame, Anyone can help plz
The most efficient solution will be MediaMetadataRetriever.
First of all call
setDataSource(FileDescriptor fd)
Yes it's quite a tricky method. Sometimes it requires complicated solution. Here is an example:
public void setVideo(Uri videoPath) {
videoUri = videoPath;
File file = new File(videoPath.getPath());
ParcelFileDescriptor pfd;
FileDescriptor fd = new FileDescriptor();
try {
pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
fd = pfd.getFileDescriptor();
metadataRetriever.setDataSource(fd);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
to set the video you want to work with. Secondly use this method:
getFrameAtTime(long timeUs)
to extract a frame. Keep in mind you should use microseconds as parameter