Java 使用FFMPEG连接两个视频文件时出现的问题
我正在尝试连接两个从gallery中获取的mp4文件。我遇到了流程执行失败的问题。我已经添加了代码和错误日志。使用来自guardian项目的ffmpeg库 我在三星Galaxy S3设备上运行这个 在这一行中抛出错误Java 使用FFMPEG连接两个视频文件时出现的问题,java,android,video,ffmpeg,media,Java,Android,Video,Ffmpeg,Media,我正在尝试连接两个从gallery中获取的mp4文件。我遇到了流程执行失败的问题。我已经添加了代码和错误日志。使用来自guardian项目的ffmpeg库 我在三星Galaxy S3设备上运行这个 在这一行中抛出错误 ProcessBuilder pb = new ProcessBuilder(cmds); pb.directory(fileExec); Process process = pb.start(); 当我用这个替换上面的最后一行时 Process process = Runt
ProcessBuilder pb = new ProcessBuilder(cmds);
pb.directory(fileExec);
Process process = pb.start();
当我用这个替换上面的最后一行时
Process process = Runtime.getRuntime().exec("chmod 777 "+cmds.toArray(new String[cmds.size()]));
它可以解决如下所示的异常。但产出似乎没有出现
文件连接代码:
File fileVideoOutput = new File(getApplicationContext()
.getExternalFilesDir("test") + "hello.mp4");
fileVideoOutput.delete();
File fileTmp = getApplicationContext().getCacheDir();
File fileAppRoot = new File(getApplicationContext()
.getApplicationInfo().dataDir);
try {
FfmpegController fc = new FfmpegController(fileTmp, fileAppRoot);
ArrayList<Clip> listVideos = new ArrayList<Clip>();
Clip clip = new Clip();
clip.path = video1;
fc.getInfo(clip);
clip.duration = clip.duration;
System.out.println("Clip1 duration " + clip.duration);
listVideos.add(clip);
Clip clip2 = new Clip();
clip2.path = video2;
fc.getInfo(clip2);
clip2.duration = clip2.duration;
System.out.println("Clip2 duration " + clip2.duration);
listVideos.add(clip2);
Clip clipOut = new Clip();
clipOut.path = fileVideoOutput.getCanonicalPath();
fc.concatAndTrimFilesMP4Stream(listVideos, clipOut, false, false,
new ShellUtils.ShellCallback() {
@Override
public void shellOut(String shellLine) {
System.out.println("fc>" + shellLine);
}
@Override
public void processComplete(int exitValue) {
if (exitValue < 0)
System.err.println("concat non-zero exit: "
+ exitValue);
}
});
} catch (Exception e1) {
e1.printStackTrace();
}
下面是我在ffmpeg教程页面中给出的ffmpeg命令
ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4
FFMPEG库似乎没有按预期工作。当我添加命令并使用“touch”创建输出文件时,正在创建shell命令文件。但我仍然无法按预期查看ffmpeg的输出。我看到您有一个java.io.IOException:权限被拒绝,您有权读取/写入应用程序清单中的文件吗? 您能显示您的AndroidManifest.xml文件吗 最好是在应用程序的临时目录中执行所有操作,然后将它们复制到外部SD卡 为了复制文件,您必须使用自定义类,这是我的:
package com.five_doors.xposedtranslate.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
/**
* A small util file class to handle things Java 6 Should have handled himself
* @author Hugo
*
*/
public class FileUtil {
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
// previous code: destination.transferFrom(source, 0, source.size());
// to avoid infinite loops, should be:
long count = 0;
long size = source.size();
while((count += destination.transferFrom(source, count, size-count))<size);
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
public static void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteRecursive(child);
fileOrDirectory.delete();
}
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
public static String getStringFromFile (String filePath) throws Exception {
File fl = new File(filePath);
FileInputStream fin = new FileInputStream(fl);
String ret = convertStreamToString(fin);
//Make sure you close all streams.
fin.close();
return ret;
}
}
package com.five_doors.xposedtranslate.util;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.nio.channels.FileChannel;
/**
*一个小的util文件类来处理Java6应该自己处理的事情
*@作者雨果
*
*/
公共类FileUtil{
公共静态void copyFile(文件sourceFile、文件destFile)引发IOException{
如果(!destFile.exists()){
destFile.createNewFile();
}
filechannelsource=null;
filechanneldestination=null;
试一试{
source=新文件输入流(sourceFile).getChannel();
destination=新文件输出流(destFile).getChannel();
//以前的代码:destination.transferFrom(source,0,source.size());
//为避免无限循环,应:
长计数=0;
long size=source.size();
while((计数+=目的地.transferFrom(源、计数、大小计数))我拥有所有这些权限。这与进程有关。因为当我更改文章顶部提到的行时,权限被拒绝的问题就会出现。但我的ffmpeg命令似乎都不起作用。@intrepidkarthi是你的video1和video2吗?可能它们在文件夹中如果您没有访问权限,请尝试将视频放在缓存目录的根目录下:final Clip out=new Clip(getApplicationContext().getCacheDir()+“/compiled.mp4”);它在SD卡中。我将尝试保留在缓存目录中并让您知道。谢谢。它与缓存目录一起工作。是否有正式的方法将文件从缓存目录复制到外部目录,您可以通过设备库应用程序查看视频?
package com.five_doors.xposedtranslate.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
/**
* A small util file class to handle things Java 6 Should have handled himself
* @author Hugo
*
*/
public class FileUtil {
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
// previous code: destination.transferFrom(source, 0, source.size());
// to avoid infinite loops, should be:
long count = 0;
long size = source.size();
while((count += destination.transferFrom(source, count, size-count))<size);
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
public static void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteRecursive(child);
fileOrDirectory.delete();
}
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
public static String getStringFromFile (String filePath) throws Exception {
File fl = new File(filePath);
FileInputStream fin = new FileInputStream(fl);
String ret = convertStreamToString(fin);
//Make sure you close all streams.
fin.close();
return ret;
}
}