您好,我正在使用下面的 jave 代码将每个 video 文件拆分为 1MB 并且它们拆分得很好,但是当我转到它们的物理本地路径并尝试播放他们没有播放,我也在使用 FFMpeg 来处理这个场景,但这对我来说也不起作用,显示异常,如 No such file or Dictionary
请帮我解决这个问题
代码:
public static List<File> splitFile(File f) {
try {
int partCounter = 1;
List<File> result = new ArrayList<>();
int sizeOfFiles = 1024 * 1024;// 1MB
byte[] buffer = new byte[sizeOfFiles];
// create a buffer of bytes sized as the one chunk size
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
String name = f.getName();
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
File newFile = new File(f.getParent(), name + "." + String.format("%03d", partCounter++));
// naming files as <inputFileName>.001, <inputFileName>.002, ...
FileOutputStream out = new FileOutputStream(newFile);
out.write(buffer, 0, tmp);//tmp is chunk size. Need it for the last chunk,
// which could be less then 1 mb.
result.add(newFile);
}
return result;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
FFMpeg代码:-
public void getSplitCommand(String inputFileUrl, String outputFileUrl) {
inputFileUrl= /storage/emulated/0/1492848702.mp4;
outputFileUrl= /storage/emulated/0/1492848702.mp4;
;
String cmd[] = new String[]{"-i ",inputFileUrl+" ","-c ","copy ","-map ","0 ",
"-segment_time ","8 ","-f ","segment/sdcard/Download/output%03d.mp4"};
executeBinaryCommand(fFmpeg, cmd);
}
/**
* executeBinaryCommand
*
* @param ffmpeg
* @param command
*/
public void executeBinaryCommand(FFmpeg ffmpeg, String[] command) {
try {
if (ffmpeg != null) {
ffmpeg.execute(command,
new ExecuteBinaryResponseHandler() {
@Override
public void onFailure(String response) {
System.out.println("failure====>" + response.toString());
}
@Override
public void onSuccess(String response) {
System.out.println("resposense====>" + response.toString());
}
@Override
public void onProgress(String response) {
System.out.println("on progress");
}
@Override
public void onStart() {
System.out.println("start");
}
@Override
public void onFinish() {
System.out.println("Finish");
}
});
}
} catch (FFmpegCommandAlreadyRunningException exception) {
exception.printStackTrace();
}
}
最佳答案
使用 [FFMPEG][2] 库分割视频。您的拆分方法可能会损坏视频。 FFMPEG 不会发生这种情况
ffmpeg -i INPUT.mp4 -acodec copy -f segment -vcodec copy -reset_timestamps 1 -map 0 OUTPUT%d.mp4
[2]: https://github.com/WritingMinds/ffmpeg-android-java/tree/master/FFmpegAndroid我将视频分成每 8 秒一次。不要分成大小。总是在持续时间内 split
关于android - 在 android 中拆分后文件不在本地播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773403/