jave2
jave2 copied to clipboard
The ffmpeg version included in 2.7.3 is a vry old one, which might cause the issue.
The ffmpeg version included in 2.7.3 is a vry old one, which might cause the issue. You could enable debug logging and see why it fails.
We don't have enough resources to support such old versions
Originally posted by @a-schild in https://github.com/a-schild/jave2/issues/160#issuecomment-909134326
Hello @a-schild . i must reopen #160 . sorry.
when i use jave-all-deps:3.2.0-SNAPSHOT.i cannot concat mp4 correctly again.
my story is :
when i do concat mp4 in 2.7.3 ,i can't concat mp4 together directly. so i google lots of pages ,finnally a method told me
- mp4 transcode to .ts
- concat .ts together
- ts transcode to mp4.
it works.:)
but now in 3.2.0-snapshot . neither directly concat mp4 nor transcode ts then concat is ok. :(
i test it on my mac. my codes below:
String tmpdir = System.getProperty("java.io.tmpdir");
public List<String> formatVideo(List<String> videoPathList, String targetFormat)
throws IllegalArgumentException,EncoderException {
AudioAttributes audio = new AudioAttributes();
audio.setCodec(DIRECT_STREAM_COPY);
VideoAttributes video = new VideoAttributes();
video.setCodec(DIRECT_STREAM_COPY);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
attrs.setEncodingThreads(2);
for (String o: videoPathList) {
String[] keySplit = o.split("/");
String fileName = keySplit[keySplit.length - 1];
String targetName = fileName.substring(0,fileName.lastIndexOf(".") + 1) + targetFormat;
File target = new File(targetPath, targetName);
if (target.exists()) {
target.delete();
}
Encoder encoder = new Encoder();
try {
encoder.encode(new MultimediaObject( new File(o)), target, attrs);
}catch (Exception e){
throw e;
}
formattedList.add(targetPath + targetName);
}
return formattedList;
}
public String concatVideo(List<String> filePathList, String concatName) throws IllegalArgumentException, InputFormatException,
EncoderException {
File target = new File(tmpdir, concatName);
if (target.exists()) {
target.delete();
}
AudioAttributes audio = new AudioAttributes();
audio.setCodec(DIRECT_STREAM_COPY);
audio.setBitRate(96000);
audio.setSamplingRate(16000);
audio.setChannels(1);
VideoAttributes video = new VideoAttributes();
video.setCodec(DIRECT_STREAM_COPY);
video.setBitRate(2045000);
video.setFrameRate(24);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setOutputFormat(concatName.substring(concatName.lastIndexOf(".") + 1));
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
attrs.setEncodingThreads(2);
Encoder encoder = new Encoder();
List<MultimediaObject> src = new ArrayList<>();
for (String o : filePathList) {
File sourceTmp = new File(o);
src.add(new MultimediaObject(sourceTmp));
}
encoder.encode(src, target, attrs);
Assert.isTrue(target.exists(), "Output file missing");
return target.getAbsoluteFile().getAbsolutePath();
}
//for instance: if you have 2 mp4 file , names are /data/a.mp4 (10M) and /data/b.mp4(5M).two mp4 filies are the same . video is h264 1280*720 .audio is aac 1ch 16khz
List<String> filePathList;
filePathList.add(' /data/a.mp4 ');
filePathList.add(' /data/b.mp4 ');
concatVideo(filePathList,'result.mp4');
//or
List<String> formattedPath = formatVideo(filePathList, "ts");
concatVideo(formattedPath,'result.mp4');
the result is only System.getProperty("java.io.tmpdir")/result.mp4 produced. only 10M~
could u test mp4 concat in 3.2.0-snapshot thank you.
After deepdiving into the code, I'm wondering if the concat did ever work properly... Basically there are two concat types ffmpeg supports:
- Concat two (or more) files, without touching metadata and doing conversions (Thats this which probably was working in the past)
- complex_filter which allows to concat and map the different streams in the input to different streams in the output I'm looking into implementing 2, since the first one only makse sense for streaming files
Can you please try the new 3.2.1-SNAPSHOT release? Please note, that you also need to pass a Filter to the encoding, so it knows how to map video and audio streams.
FilterGraph complexFiltergraph= new FilterGraph();
FilterChain fc= new FilterChain();
fc.addFilter(new MediaConcatFilter(src.size()));
complexFiltergraph.addChain(fc);
video.setComplexFiltergraph(complexFiltergraph);
Can you please try the new 3.2.1-SNAPSHOT release? Please note, that you also need to pass a Filter to the encoding, so it knows how to map video and audio streams.
FilterGraph complexFiltergraph= new FilterGraph(); FilterChain fc= new FilterChain(); fc.addFilter(new MediaConcatFilter(src.size())); complexFiltergraph.addChain(fc); video.setComplexFiltergraph(complexFiltergraph);
i have updated to 3.2.1-snapshot and i change my code below:
public String concatVideo(List<String> filePathList, String concatName) throws IllegalArgumentException, InputFormatException,
EncoderException {
File target = new File(tmpdir, concatName);
if (target.exists()) {
target.delete();
}
AudioAttributes audio = new AudioAttributes();
audio.setCodec(DIRECT_STREAM_COPY);
audio.setBitRate(96000);
audio.setSamplingRate(16000);
audio.setChannels(1);
VideoAttributes video = new VideoAttributes();
video.setCodec(DIRECT_STREAM_COPY);
video.setBitRate(2045000);
video.setFrameRate(24);
//a Filter to encoding codes added
FilterGraph complexFiltergraph= new FilterGraph();
FilterChain fc= new FilterChain();
fc.addFilter(new MediaConcatFilter(filePathList.size()));
complexFiltergraph.addChain(fc);
video.setComplexFiltergraph(complexFiltergraph);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setOutputFormat(concatName.substring(concatName.lastIndexOf(".") + 1));
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
attrs.setEncodingThreads(2);
Encoder encoder = new Encoder();
List<MultimediaObject> src = new ArrayList<>();
for (String o : filePathList) {
File sourceTmp = new File(o);
src.add(new MultimediaObject(sourceTmp));
}
encoder.encode(src, target, attrs);
Assert.isTrue(target.exists(), "Output file missing");
return target.getAbsoluteFile().getAbsolutePath();
}
1.concat mp4 directly: my two mp4 are 22M and 16M respectively. it last 1 hour and result.mp4 is 160M and still processing. the result must be wrong so i terminate the process.
2.first transcode to .ts and then concat and transcodde to .mp4 to .ts very fast , but trancode to mp4 very slow , it last 10min and result.mp4 is 42M and still processing. i think the result must be wrong again so i terminate the process too.
finally, i think add Filter is not a better method . because it take time longger , file bigger , resuoce taken more. and it seem not stop by itself.
could u try concat your mp4 for test and show ur test result and process detail?
It's been 9 hours since MY last reply. i closed the jave thread and close my mac screen and dealing with my home stuff,now i return , i found my mac power very low (11%) and its fans run fast make a big noise. i open "Activity monitor" see 3 ffmpeg process are still running. so do you think it is a bug?
In the testConcatVideo2() unit test, the concat of two mp4 files work as expected
https://github.com/a-schild/jave2/blob/develop/jave-core-test/src/test/java/ws/schild/jave/ConcatEncoderTest.java
i wil check later. thx your work
HI @a-schild.
- i have a question about the sample code here:
video.setFrameRate(30);
video.setSize(new VideoSize(320, 240));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setOutputFormat("mp4");
attrs.setVideoAttributes(video);
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
List<MultimediaObject> src = new ArrayList<>();
src.add(new MultimediaObject(source1));
src.add(new MultimediaObject(source2));
//why these code below attrs.setVideoAttributes(video);?
FilterGraph complexFiltergraph= new FilterGraph();
FilterChain fc= new FilterChain();
fc.addFilter(new MediaConcatFilter(src.size()));
complexFiltergraph.addChain(fc);
video.setComplexFiltergraph(complexFiltergraph);
2.i have two mp4 videos.
the first one 18M and ffprobe information is:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1_1630478805&1719_1630478247_1630478546.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2021-09-01T06:42:23.000000Z
com.android.version: 9
Duration: 00:04:59.63, start: 0.000000, bitrate: 491 kb/s
Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 96 kb/s (default)
Metadata:
creation_time : 2021-09-01T06:42:23.000000Z
handler_name : SoundHandle
vendor_id : [0][0][0][0]
Stream #0:1(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 396 kb/s, SAR 1:1 DAR 16:9, 4.71 fps, 90k tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2021-09-01T06:42:23.000000Z
handler_name : VideoHandle
vendor_id : [0][0][0][0]
the scond one 21M and ffprobe information is:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1_1630479729&1719_1630478547_1630478848.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2021-09-01T06:47:25.000000Z
com.android.version: 9
Duration: 00:04:59.78, start: 0.000000, bitrate: 593 kb/s
Stream #0:0(eng): Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 96 kb/s (default)
Metadata:
creation_time : 2021-09-01T06:47:25.000000Z
handler_name : SoundHandle
vendor_id : [0][0][0][0]
Stream #0:1(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720, 496 kb/s, SAR 1:1 DAR 16:9, 5.78 fps, 90k tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2021-09-01T06:47:25.000000Z
handler_name : VideoHandle
vendor_id : [0][0][0][0]
now , the concat work is still running . the concated mp4 video is 104M and is still growing.my mac's fan is making big noise.
i use the jave version is 3.2.1-snapshot
implementation 'ws.schild:jave-all-deps:3.2.1-SNAPSHOT'
and my code below here:
public String concatVideo(Long taskId, List<String> filePathList, DirType targetDir, String concatName) throws IllegalArgumentException, InputFormatException,
EncoderException {
String targetDirPath = fileInfra.makeDirPath(taskId, targetDir);
File target = new File(targetDirPath, concatName);
if (target.exists()) {
target.delete();
}
AudioAttributes audio = new AudioAttributes();
FilterGraph complexFiltergraph= new FilterGraph();
FilterChain fc= new FilterChain();
fc.addFilter(new MediaConcatFilter(filePathList.size()));
complexFiltergraph.addChain(fc);
video.setComplexFiltergraph(complexFiltergraph);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setOutputFormat(concatName.substring(concatName.lastIndexOf(".") + 1));
attrs.setOutputFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
attrs.setEncodingThreads(2);
Encoder encoder = new Encoder();
List<MultimediaObject> src = new ArrayList<>();
for (String o : filePathList) {
File sourceTmp = new File(o);
src.add(new MultimediaObject(sourceTmp));
}
encoder.encode(src, target, attrs);
Assert.isTrue(target.exists(), "Output file missing");
return target.getAbsoluteFile().getAbsolutePath();
}