Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.dadiyang</groupId>
<groupId>com.github.zywayh</groupId>
<artifactId>jave</artifactId>
<packaging>jar</packaging>
<version>1.0.6</version>
<version>1.0.7</version>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>9</version>
</parent>
<licenses>
<license>
<name>GNU General Public License (GPL)</name>
<url>http://www.gnu.org/licenses/gpl.txt</url>
</license>
</licenses>
<scm>
<url>https://github.com/dadiyang/jave</url>
<connection>https://github.com/dadiyang/jave.git</connection>
<developerConnection>https://github.com/dadiyang/jave</developerConnection>
</scm>
<developers>
<developer>
<name>Xuyang Huang</name>
<email>dadiyang@aliyun.com</email>
<url>https://github.com/dadiyang/jave</url>
</developer>
</developers>
<!--<licenses>-->
<!--<license>-->
<!--<name>GNU General Public License (GPL)</name>-->
<!--<url>http://www.gnu.org/licenses/gpl.txt</url>-->
<!--</license>-->
<!--</licenses>-->
<!--<scm>-->
<!--<url>https://github.com/dadiyang/jave</url>-->
<!--<connection>https://github.com/dadiyang/jave.git</connection>-->
<!--<developerConnection>https://github.com/dadiyang/jave</developerConnection>-->
<!--</scm>-->
<!--<developers>-->
<!--<developer>-->
<!--<name>Xuyang Huang</name>-->
<!--<email>dadiyang@aliyun.com</email>-->
<!--<url>https://github.com/dadiyang/jave</url>-->
<!--</developer>-->
<!--</developers>-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.25</slf4j.version>
Expand All @@ -54,7 +54,9 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>jave</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -73,4 +75,14 @@
</plugins>
</build>

<!-- nexus私服配置 start -->
<distributionManagement>
<repository>
<id>releases</id>
<url>http://47.95.13.52:16500/repository/maven-releases/</url>
</repository>
</distributionManagement>
<!-- nexus私服配置 start -->


</project>
3 changes: 0 additions & 3 deletions src/main/java/it/sauronsoftware/jave/AudioUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public static void amrToWav(File source, File target) {
}

public static void convert(File source, File target, String format) {
if (!source.exists()) {
throw new IllegalArgumentException("source file does not exists: " + source.getAbsoluteFile());
}
AudioAttributes audio = new AudioAttributes();
Encoder encoder = new IgnoreErrorEncoder();
audio.setCodec(LIBMP_3_LAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public DefaultFFMPEGLocator() {
}

@Override
protected String getFFMPEGExecutablePath() {
public String getFFMPEGExecutablePath() {
return path;
}

Expand Down
98 changes: 39 additions & 59 deletions src/main/java/it/sauronsoftware/jave/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -739,6 +739,9 @@ public void encode(File source, File target, EncodingAttributes attributes)
public void encode(File source, File target, EncodingAttributes attributes,
EncoderProgressListener listener) throws IllegalArgumentException,
InputFormatException, EncoderException {
if (!source.exists()) {
throw new IllegalArgumentException("source file does not exists: " + source.getAbsoluteFile());
}
String formatAttribute = attributes.getFormat();
Float offsetAttribute = attributes.getOffset();
Float durationAttribute = attributes.getDuration();
Expand Down Expand Up @@ -787,8 +790,7 @@ public void encode(File source, File target, EncodingAttributes attributes,
VideoSize size = videoAttributes.getSize();
if (size != null) {
ffmpeg.addArgument("-s");
ffmpeg.addArgument(String.valueOf(size.getWidth()) + "x"
+ String.valueOf(size.getHeight()));
ffmpeg.addArgument(size.getWidth() + "x" + size.getHeight());
}
}
if (audioAttributes == null) {
Expand Down Expand Up @@ -820,10 +822,13 @@ public void encode(File source, File target, EncodingAttributes attributes,
ffmpeg.addArgument(String.valueOf(volume.intValue()));
}
}

ffmpeg.addArgument("-f");
ffmpeg.addArgument(formatAttribute);
ffmpeg.addArgument("-y");
ffmpeg.addArgument(target.getAbsolutePath());

//根据不同的format插入不同的命令参数
FormatEnum.get(formatAttribute).handle(ffmpeg, target);

try {
ffmpeg.execute();
} catch (IOException e) {
Expand All @@ -839,9 +844,7 @@ public void encode(File source, File target, EncodingAttributes attributes,
}
}


protected void processErrorOutput(EncodingAttributes attributes, BufferedReader errorReader, File source, EncoderProgressListener listener) throws EncoderException, IOException {
String lastWarning = null;
long progress = 0L;
Float offsetAttribute = attributes.getOffset();
MultimediaInfo info = parseMultimediaInfo(source, (RBufferedReader)errorReader);
Expand All @@ -862,76 +865,53 @@ protected void processErrorOutput(EncodingAttributes attributes, BufferedReader
}
int step = 0;
String line;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
Calendar calendar = Calendar.getInstance();
while ((line = errorReader.readLine()) != null) {
if (step == 0) {
if (line.startsWith("WARNING: ")) {
if (listener != null)
listener.message(line);
if (listener != null) listener.message(line);
} else {
if (!line.startsWith("Output #0")) {
throw new EncoderException(line);
}
if (!line.startsWith("Output #0"))
if (listener != null) listener.message(line);
step++;
}
} else if ((step == 1) &&
(!line.startsWith(" "))) {
} else if ((step == 1) && (!line.startsWith(" "))) {
step++;
}

if (step == 2) {
if (!line.startsWith("Stream mapping:")) {
throw new EncoderException(line);
}
} else if (step == 2) {
if (!line.startsWith("Stream mapping:"))
if (listener != null) listener.message(line);
step++;
}
else if ((step == 3) &&
(!line.startsWith(" "))) {
} else if ((step == 3) && (!line.startsWith(" "))) {
step++;
}

if (step == 4) {
} else if (step == 4) {
line = line.trim();
if (line.length() > 0) {
Hashtable table = parseProgressInfoLine(line);
if (table == null) {
if (listener != null) {
listener.message(line);
}
lastWarning = line;
} else {
if (table != null) {
if (listener != null) {
String time = (String)table.get("time");
if (time != null) {
int dot = time.indexOf(46);
if ((dot > 0) && (dot == time.length() - 2) && (duration > 0L))
{
String p1 = time.substring(0, dot);
String p2 = time.substring(dot + 1);
try {
long i1 = Long.parseLong(p1);
long i2 = Long.parseLong(p2);
progress = i1 * 1000L + i2 * 100L;

int perm = (int)Math.round(progress * 1000L / duration);

if (perm > 1000) {
perm = 1000;
}
listener.progress(perm);
}
catch (NumberFormatException e) {
}
if (time != null && (duration > 0L)) {
try {
Date data = simpleDateFormat.parse(time);
calendar.setTime(data);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
long i1 = (second + minute * 60 + hour * 60 * 60);
progress = i1 * 1000L;
int perm = Math.round(progress * 1000L / duration);
if (perm > 1000) perm = 1000;
listener.progress(perm);
} catch (Exception e) {
e.printStackTrace();
}

}
}
lastWarning = null;
}
}
}
}
if ((lastWarning != null) &&
(!SUCCESS_PATTERN.matcher(lastWarning).matches()))
throw new EncoderException(lastWarning);
}

}
2 changes: 1 addition & 1 deletion src/main/java/it/sauronsoftware/jave/FFMPEGExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author Carlo Pelliccia
*/
class FFMPEGExecutor {
public class FFMPEGExecutor {
private static final Logger log = LoggerFactory.getLogger(FFMPEGExecutor.class);
/**
* The path of the ffmpeg executable.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/it/sauronsoftware/jave/FFMPEGLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class FFMPEGLocator {
*
* @return The path of the ffmpeg executable.
*/
protected abstract String getFFMPEGExecutablePath();
public abstract String getFFMPEGExecutablePath();

/**
* It returns a brand new {@link FFMPEGExecutor}, ready to be used in a
Expand All @@ -42,7 +42,7 @@ public abstract class FFMPEGLocator {
* @return A newly instanced {@link FFMPEGExecutor}, using this locator to
* call the ffmpeg executable.
*/
FFMPEGExecutor createExecutor() {
public FFMPEGExecutor createExecutor() {
return new FFMPEGExecutor(getFFMPEGExecutablePath());
}

Expand Down
52 changes: 52 additions & 0 deletions src/main/java/it/sauronsoftware/jave/FormatEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package it.sauronsoftware.jave;

import java.io.File;

/**
* @description:
* @author: zhuyawei
* @date: 2020-01-08 13:39
*/
public enum FormatEnum {
def(){
@Override
public void handle(FFMPEGExecutor ffmpeg, File target) {
ffmpeg.addArgument("-y");
ffmpeg.addArgument(target.getAbsolutePath());
}
},
ssegment(){
@Override
public void handle(FFMPEGExecutor ffmpeg, File target) {
ffmpeg.addArgument("-map");
ffmpeg.addArgument("0");
ffmpeg.addArgument("-segment_format");
ffmpeg.addArgument("mpegts");
ffmpeg.addArgument("-segment_list");
if(!target.getParentFile().exists())target.getParentFile().mkdirs();
ffmpeg.addArgument(target.getAbsolutePath());//拼接存放目录
ffmpeg.addArgument("-segment_time");
ffmpeg.addArgument("10");
ffmpeg.addArgument(target.getParent() + File.separator + "%03d.ts");//拼接存放目录
}
};

FormatEnum() {}

public abstract void handle(FFMPEGExecutor ffmpeg, File target);

public static FormatEnum get(String name){
if(name == null || name.isEmpty()) return def;
try{
return FormatEnum.valueOf(name);
}catch (IllegalArgumentException e){
return def;
}
}

public static void main(String[] args) {

System.out.println(FormatEnum.get("ssegment"));
}

}
Loading