Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
aliyun-fc-service
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
吴展材
aliyun-fc-service
Commits
bb9af247
Commit
bb9af247
authored
Jun 15, 2023
by
wuzhancai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feature:(阿里云函数式-短视频抽帧及短视频音频抽取)
parents
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
1204 additions
and
0 deletions
+1204
-0
pom.xml
pom.xml
+96
-0
AwemeAudioDownloadOss.java
...n/java/com/afanticar/aliyun/fc/AwemeAudioDownloadOss.java
+187
-0
AwemeAudioFrameOss.java
...main/java/com/afanticar/aliyun/fc/AwemeAudioFrameOss.java
+218
-0
HelloFC.java
src/main/java/com/afanticar/aliyun/fc/HelloFC.java
+147
-0
AwemeSimpleRequest.java
...m/afanticar/aliyun/fc/dto/request/AwemeSimpleRequest.java
+82
-0
SimpleRequest.java
...va/com/afanticar/aliyun/fc/dto/request/SimpleRequest.java
+33
-0
AwemeCallbackContent.java
...fanticar/aliyun/fc/dto/response/AwemeCallbackContent.java
+72
-0
SimpleResponse.java
.../com/afanticar/aliyun/fc/dto/response/SimpleResponse.java
+23
-0
FFmpegUtil.java
src/main/java/com/afanticar/aliyun/fc/util/FFmpegUtil.java
+316
-0
ffmpeg
src/main/resources/ffmpeg
+0
-0
log4j.properties
src/main/resources/log4j.properties
+30
-0
No files found.
pom.xml
0 → 100644
View file @
bb9af247
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
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.afanticar
</groupId>
<version>
1.0.0
</version>
<artifactId>
aliyun-fc-service
</artifactId>
<name>
${project.artifactId}
</name>
<description>
阿里云函数式-配置以及启动模块
</description>
<properties>
<maven.compiler.source>
8
</maven.compiler.source>
<maven.compiler.target>
8
</maven.compiler.target>
<maven.test.skip>
true
</maven.test.skip>
</properties>
<dependencies>
<dependency>
<groupId>
com.aliyun.oss
</groupId>
<artifactId>
aliyun-sdk-oss
</artifactId>
<version>
3.10.2
</version>
</dependency>
<dependency>
<groupId>
com.aliyun.fc.runtime
</groupId>
<artifactId>
fc-java-core
</artifactId>
<version>
1.4.1
</version>
</dependency>
<!-- <dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.4</version>
</dependency>-->
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-api
</artifactId>
<version>
1.7.5
</version>
</dependency>
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-log4j12
</artifactId>
<version>
1.7.5
</version>
</dependency>
<dependency>
<groupId>
log4j
</groupId>
<artifactId>
log4j
</artifactId>
<version>
1.2.12
</version>
</dependency>
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
<version>
5.5.2
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.28
</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-shade-plugin
</artifactId>
<version>
3.2.1
</version>
<executions>
<execution>
<phase>
package
</phase>
<goals>
<goal>
shade
</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>
*:*
</artifact>
<excludes>
<exclude>
META-INF/*.SF
</exclude>
<exclude>
META-INF/*.DSA
</exclude>
<exclude>
META-INF/*.RSA
</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
src/main/java/com/afanticar/aliyun/fc/AwemeAudioDownloadOss.java
0 → 100644
View file @
bb9af247
package
com
.
afanticar
.
aliyun
.
fc
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.http.HttpUtil
;
import
com.afanticar.aliyun.fc.dto.request.AwemeSimpleRequest
;
import
com.afanticar.aliyun.fc.dto.response.AwemeCallbackContent
;
import
com.afanticar.aliyun.fc.util.FFmpegUtil
;
import
com.alibaba.fastjson.JSONObject
;
import
com.aliyun.fc.runtime.Context
;
import
com.aliyun.fc.runtime.Credentials
;
import
com.aliyun.fc.runtime.PojoRequestHandler
;
import
com.aliyun.oss.OSS
;
import
com.aliyun.oss.OSSClientBuilder
;
import
com.aliyun.oss.model.PutObjectResult
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.File
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.util.*
;
/**
* @Author: wuzhancai
* @Desc: 下载视频-上传oss
* @Date: 2023/6/2 13:51
*/
public
class
AwemeAudioDownloadOss
implements
PojoRequestHandler
<
AwemeSimpleRequest
,
AwemeCallbackContent
>
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
AwemeAudioDownloadOss
.
class
);
@Override
public
AwemeCallbackContent
handleRequest
(
AwemeSimpleRequest
request
,
Context
context
)
{
AwemeCallbackContent
message
=
getAudio
(
request
,
context
);
return
message
;
}
public
static
void
main
(
String
[]
args
)
{
AwemeSimpleRequest
request
=
new
AwemeSimpleRequest
();
request
.
setAwemeId
(
"3454335345"
);
request
.
setAwemeUrl
(
"https://api-hl.amemv.com/aweme/v1/play/?video_id=v0d00fg10000chqub8bc77u0q4inptmg&line=1&file_id=ef3e15d11fae40db94f49c8fb3be11d7&sign=8ae7630595ecd49fb598e5bbae9b19a4&is_play_url=1&source=PackSourceEnum_PUBLISH"
);
// request.setContentCenterUrl("https://api-dev.afanticar.com/content-center/v1/kaize/publish/task");
request
.
setContentCenterUrl
(
"http://localhost:18902/v1/aweme/recognize"
);
Date
beginDate
=
new
Date
();
log
.
info
(
"begin time {}"
,
beginDate
);
AwemeCallbackContent
audio
=
getAudio
(
request
,
null
);
Date
endDate
=
new
Date
();
log
.
info
(
"end time {} user {} "
,
endDate
,(
endDate
.
getTime
()-
beginDate
.
getTime
()));
System
.
out
.
println
(
audio
);
}
private
static
AwemeCallbackContent
getAudio
(
AwemeSimpleRequest
request
,
Context
context
)
{
// String url = "https://api.amemv.com/aweme/v1/play/?video_id=v0200fg10000cgbkfqjc77u3udkfff30&line=1&file_id=db82e40c28ad49fb860a247ace95f8b8&sign=44b50f681b684101c3f33ce7c25ffd02&is_play_url=1&source=PackSourceEnum_PUBLISH";
String
url
=
request
.
getAwemeUrl
();
String
awemeId
=
request
.
getAwemeId
();
awemeId
=
UUID
.
randomUUID
().
toString
();
String
folderPath
=
"/code/temp/aweme"
;
String
fileName
=
awemeId
+
".mp4"
;
String
localWavPath
=
"/code/temp/"
+
fileName
;
String
ossAudioFolderPath
=
"recognize/audio/"
;
String
ossAwemeFolderPath
=
"recognize/aweme/"
;
String
ossFrameFolderPath
=
"recognize/frame/"
+
awemeId
+
"/"
;
String
localFrameFolderPath
=
"/code/temp/aweme/"
+
awemeId
;
File
folder
=
new
File
(
localFrameFolderPath
);
if
(!
folder
.
exists
()){
folder
.
mkdirs
();
}
// Bucket名称, 需要预先创建。
String
bucketName
=
"cont-aweme-dev"
;
// Endpoint必须填写Bucket所在地域对应的Endpoint,推荐使用内网访问地址。以华东1(杭州)为例,内网访问Endpoint为https://oss-cn-hangzhou-internal.aliyuncs.com。
String
endpoint
=
"https://oss-cn-hangzhou-internal.aliyuncs.com"
;
// 获取密钥信息,执行前,确保函数所在的服务配置了角色信息,并且角色需要拥有AliyunOSSFullAccess权限。
// 建议直接使用AliyunFCDefaultRole角色。
String
os
=
System
.
getProperty
(
"os.name"
);
if
(
os
.
toLowerCase
().
indexOf
(
"win"
)!=-
1
){
endpoint
=
"https://oss-cn-hangzhou.aliyuncs.com"
;
}
// log.info("context {}", context);
/*Credentials creds = null;
if(context != null){
creds = context.getExecutionCredentials();
log.info("creds.getAccessKeyId{},creds.getAccessKeySecret{},creds.getSecurityToken{}",creds.getAccessKeyId(),creds.getAccessKeySecret(),creds.getSecurityToken());
}*/
//下载视频
Date
beginDate
=
new
Date
();
log
.
info
(
"date begin time {}"
,
beginDate
);
HttpUtil
.
downloadFile
(
getTransferUrl
(
url
),
localWavPath
);
Date
endDate
=
new
Date
();
log
.
info
(
"date end time {}"
,
endDate
);
log
.
info
(
"sheng yu {}"
,
endDate
.
getTime
()-
beginDate
.
getTime
());
OSS
ossClient
=
null
;
if
(
context
!=
null
){
// ossClient = new OSSClientBuilder().build(endpoint, creds.getAccessKeyId(), creds.getAccessKeySecret(),creds.getSecurityToken());
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
"LTAI5tGcLYurZDbTExbdFAZL"
,
"Ji3DsPdK6u2cHup7IdxolR1CpCqaPY"
);
}
else
{
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
"LTAI5tGcLYurZDbTExbdFAZL"
,
"Ji3DsPdK6u2cHup7IdxolR1CpCqaPY"
);
}
// 填写Byte数组。
byte
[]
content
=
"Hello FC"
.
getBytes
();
File
localAwemePathfile
=
new
File
(
localWavPath
);
PutObjectResult
putObjectResult
=
ossClient
.
putObject
(
bucketName
,
ossAwemeFolderPath
+
fileName
,
localAwemePathfile
);
Date
uploadDate
=
new
Date
();
log
.
info
(
"上传时间{}"
,
uploadDate
.
getTime
()-
beginDate
.
getTime
());
// 关闭OSSClient。
ossClient
.
shutdown
();
//上传完 把本地文件删掉
localAwemePathfile
.
delete
();
AwemeCallbackContent
awemeContent
=
new
AwemeCallbackContent
();
awemeContent
.
setAwemeAudioUrl
(
ossAwemeFolderPath
+
fileName
);
String
post
=
HttpUtil
.
post
(
request
.
getContentCenterUrl
(),
JSONObject
.
toJSONString
(
awemeContent
));
log
.
info
(
"post {}"
,
post
);
// gc();
return
awemeContent
;
}
public
static
String
getTransferUrl
(
String
url
)
{
if
(
url
.
indexOf
(
"PUBLISH"
)
>
0
)
{
String
awemeUrl
=
url
;
HttpURLConnection
conn
=
null
;
try
{
conn
=
(
HttpURLConnection
)
new
URL
(
awemeUrl
).
openConnection
();
conn
.
setInstanceFollowRedirects
(
false
);
conn
.
setConnectTimeout
(
5000
);
String
location
=
conn
.
getHeaderField
(
"location"
);
if
(
StrUtil
.
isEmpty
(
location
))
{
log
.
info
(
"中转链接为空url{},状态码{},conn为{}"
,
url
,
conn
.
getResponseCode
(),
conn
);
}
return
location
;
}
catch
(
Exception
e
){
log
.
error
(
"转抛错"
,
e
);
}
}
return
url
;
}
/**
* gc clean current dump
*/
private
static
void
gc
()
{
//获取当前堆的大小 byte 单位
long
heapSize
=
Runtime
.
getRuntime
().
totalMemory
();
log
.
info
(
"Current heap size{}m"
,
heapSize
/
1024
/
1024
);
//获取堆的最大大小byte单位
//超过将抛出 OutOfMemoryException
long
heapMaxSize
=
Runtime
.
getRuntime
().
maxMemory
();
log
.
info
(
"Gets the maximum heap size{}m"
,
heapMaxSize
/
1024
/
1024
);
//获取当前空闲的内存容量byte单位
long
heapFreeSize
=
Runtime
.
getRuntime
().
freeMemory
();
log
.
info
(
"Gets the current free memory capacity{}m"
,
heapFreeSize
/
1024
/
1024
);
System
.
gc
();
log
.
info
(
"gc-after"
);
heapSize
=
Runtime
.
getRuntime
().
totalMemory
();
log
.
info
(
"Current heap size{}m"
,
heapSize
/
1024
/
1024
);
//获取堆的最大大小byte单位
//超过将抛出 OutOfMemoryException
heapMaxSize
=
Runtime
.
getRuntime
().
maxMemory
();
log
.
info
(
"Gets the maximum heap size{}m"
,
heapMaxSize
/
1024
/
1024
);
//获取当前空闲的内存容量byte单位
heapFreeSize
=
Runtime
.
getRuntime
().
freeMemory
();
log
.
info
(
"Gets the current free memory capacity{}m"
,
heapFreeSize
/
1024
/
1024
);
System
.
gc
();
}
}
src/main/java/com/afanticar/aliyun/fc/AwemeAudioFrameOss.java
0 → 100644
View file @
bb9af247
package
com
.
afanticar
.
aliyun
.
fc
;
import
cn.hutool.core.io.FileUtil
;
import
cn.hutool.http.HttpUtil
;
import
com.afanticar.aliyun.fc.dto.response.AwemeCallbackContent
;
import
com.afanticar.aliyun.fc.dto.request.AwemeSimpleRequest
;
import
com.afanticar.aliyun.fc.util.FFmpegUtil
;
import
com.alibaba.fastjson.JSONObject
;
import
com.aliyun.fc.runtime.Context
;
import
com.aliyun.fc.runtime.Credentials
;
import
com.aliyun.fc.runtime.PojoRequestHandler
;
import
com.aliyun.oss.OSS
;
import
com.aliyun.oss.OSSClientBuilder
;
import
com.aliyun.oss.model.PutObjectResult
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.File
;
import
java.util.*
;
import
java.util.concurrent.CompletableFuture
;
/**
* @Author: wuzhancai
* @Desc: 视频音频 及 抽帧
* @Date: 2023/6/2 13:51
*/
public
class
AwemeAudioFrameOss
implements
PojoRequestHandler
<
AwemeSimpleRequest
,
AwemeCallbackContent
>
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
AwemeAudioFrameOss
.
class
);
private
static
String
ffmpegPath
=
"/code/ffmpeg"
;
static
{
try
{
String
result
=
FFmpegUtil
.
assignmentFfmpegAuthority
(
ffmpegPath
);
log
.
info
(
"result {}"
,
result
);
}
catch
(
Exception
e
){
log
.
error
(
"ffmpeg init fail {}"
,
e
);
}
}
@Override
public
AwemeCallbackContent
handleRequest
(
AwemeSimpleRequest
request
,
Context
context
)
{
AwemeCallbackContent
message
=
getAudio
(
request
,
context
);
return
message
;
}
public
static
void
main
(
String
[]
args
)
{
AwemeSimpleRequest
request
=
new
AwemeSimpleRequest
();
request
.
setAwemeId
(
"3454335345"
);
request
.
setAwemeUrl
(
"https://api-hl.amemv.com/aweme/v1/play/?video_id=v0d00fg10000chqub8bc77u0q4inptmg&line=1&file_id=ef3e15d11fae40db94f49c8fb3be11d7&sign=8ae7630595ecd49fb598e5bbae9b19a4&is_play_url=1&source=PackSourceEnum_PUBLISH"
);
// request.setContentCenterUrl("https://api-dev.afanticar.com/content-center/v1/kaize/publish/task");
request
.
setContentCenterUrl
(
"http://localhost:18902/v1/aweme/recognize"
);
Date
beginDate
=
new
Date
();
log
.
info
(
"begin time {}"
,
beginDate
);
AwemeCallbackContent
audio
=
getAudio
(
request
,
null
);
Date
endDate
=
new
Date
();
log
.
info
(
"end time {} user {} "
,
endDate
,(
endDate
.
getTime
()-
beginDate
.
getTime
()));
System
.
out
.
println
(
audio
);
}
private
static
AwemeCallbackContent
getAudio
(
AwemeSimpleRequest
request
,
Context
context
)
{
String
url
=
request
.
getAwemeUrl
();
String
awemeId
=
request
.
getAwemeId
();
String
fileName
=
awemeId
+
".wav"
;
String
localWavPath
=
"/code/temp/"
+
fileName
;
String
ossAudioFolderPath
=
"recognize/audio/"
;
String
ossFrameFolderPath
=
"recognize/frame/"
+
awemeId
+
"/"
;
String
localFrameFolderPath
=
"/code/temp//aweme/"
+
awemeId
;
// Bucket名称, 需要预先创建。
String
bucketName
=
request
.
getBucketName
();
File
folder
=
new
File
(
localFrameFolderPath
);
if
(!
folder
.
exists
()){
folder
.
mkdirs
();
}
// Endpoint必须填写Bucket所在地域对应的Endpoint,推荐使用内网访问地址。以华东1(杭州)为例,内网访问Endpoint为https://oss-cn-hangzhou-internal.aliyuncs.com。
String
endpoint
=
"https://oss-cn-hangzhou-internal.aliyuncs.com"
;
// 获取密钥信息,执行前,确保函数所在的服务配置了角色信息,并且角色需要拥有AliyunOSSFullAccess权限。
// 建议直接使用AliyunFCDefaultRole角色。
String
os
=
System
.
getProperty
(
"os.name"
);
if
(
os
.
toLowerCase
().
indexOf
(
"win"
)!=-
1
){
ffmpegPath
=
"ffmpeg"
;
endpoint
=
"https://oss-cn-hangzhou.aliyuncs.com"
;
}
log
.
info
(
"context {}"
,
context
);
Credentials
creds
=
null
;
if
(
context
!=
null
){
creds
=
context
.
getExecutionCredentials
();
log
.
info
(
"creds.getAccessKeyId{},creds.getAccessKeySecret{},creds.getSecurityToken{}"
,
creds
.
getAccessKeyId
(),
creds
.
getAccessKeySecret
(),
creds
.
getSecurityToken
());
}
OSS
ossClient
=
null
;
if
(
context
!=
null
){
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
creds
.
getAccessKeyId
(),
creds
.
getAccessKeySecret
(),
creds
.
getSecurityToken
());
}
else
{
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
"LTAI5t9vrwhiU9RKYsggDqyd"
,
"S8OvXGgRCZWE51WBNw16izJZ2CfQDT"
);
}
//放音频文件
OSS
finalOssClient
=
ossClient
;
CompletableFuture
<
Boolean
>
audioFuture
=
CompletableFuture
.
supplyAsync
(()
->
putAudioFile
(
url
,
fileName
,
localWavPath
,
ossAudioFolderPath
,
bucketName
,
finalOssClient
));
//抽帧及放文件
CompletableFuture
<
List
<
String
>>
picFuture
=
CompletableFuture
.
supplyAsync
(()
->
putPicFrame
(
url
,
ossFrameFolderPath
,
localFrameFolderPath
,
bucketName
,
finalOssClient
));
List
<
String
>
frames
=
null
;
try
{
frames
=
picFuture
.
get
();
}
catch
(
Exception
e
)
{
log
.
error
(
"抽帧报错{}"
,
e
);
}
CompletableFuture
.
allOf
(
audioFuture
,
picFuture
);
// 关闭OSSClient。
ossClient
.
shutdown
();
AwemeCallbackContent
awemeContent
=
new
AwemeCallbackContent
();
awemeContent
.
setAuthorId
(
request
.
getAuthorId
());
awemeContent
.
setPlatform
(
request
.
getPlatform
());
awemeContent
.
setAwemeId
(
awemeId
);
awemeContent
.
setAwemeAudioUrl
(
ossAudioFolderPath
+
fileName
);
awemeContent
.
setAwemeFrames
(
frames
);
awemeContent
.
setPublishTime
(
request
.
getPublishTime
());
String
post
=
HttpUtil
.
post
(
request
.
getContentCenterUrl
(),
JSONObject
.
toJSONString
(
awemeContent
));
log
.
info
(
"post {}"
,
post
);
gc
();
return
awemeContent
;
}
private
static
List
<
String
>
putPicFrame
(
String
url
,
String
ossFrameFolderPath
,
String
localFrameFolderPath
,
String
bucketName
,
OSS
ossClient
)
{
List
<
String
>
frames
=
new
ArrayList
<>();
String
localFolderPath
=
localFrameFolderPath
;
FFmpegUtil
.
getFramesByPic
(
ffmpegPath
,
null
,
null
,
url
,
localFolderPath
,
"1/0.5"
,
null
,
null
);
long
halfSecond
=
500L
;
File
localFolderPathFile
=
new
File
(
localFolderPath
);
if
(
localFolderPathFile
.
exists
())
{
File
[]
files
=
localFolderPathFile
.
listFiles
();
List
<
File
>
fileList
=
Arrays
.
asList
(
files
);
//文件名进行升序排序
Collections
.
sort
(
fileList
,
new
Comparator
<
File
>()
{
@Override
public
int
compare
(
File
o1
,
File
o2
)
{
return
o1
.
getName
().
compareTo
(
o2
.
getName
());
}
});
long
secondName
=
0
;
for
(
int
i
=
0
;
i
<
fileList
.
size
();
i
++)
{
File
file
=
fileList
.
get
(
i
);
secondName
=
halfSecond
*
(
i
+
1
);
//上传到oss里
//上传文件
String
ossFramePath
=
ossFrameFolderPath
+
secondName
+
".jpg"
;
ossClient
.
putObject
(
bucketName
,
ossFramePath
,
file
);
frames
.
add
(
ossFramePath
);
}
}
FileUtil
.
del
(
localFolderPathFile
);
return
frames
;
}
private
static
Boolean
putAudioFile
(
String
url
,
String
fileName
,
String
localWavPath
,
String
ossAudioFolderPath
,
String
bucketName
,
OSS
ossClient
)
{
// 填写Byte数组。
FFmpegUtil
.
live2Audio
(
ffmpegPath
,
url
,
localWavPath
,
null
,
null
);
File
localWavPathfile
=
new
File
(
localWavPath
);
PutObjectResult
putObjectResult
=
ossClient
.
putObject
(
bucketName
,
ossAudioFolderPath
+
fileName
,
localWavPathfile
);
//上传完删除文件
localWavPathfile
.
delete
();
return
true
;
}
/**
* gc clean current dump
*/
private
static
void
gc
()
{
//获取当前堆的大小 byte 单位
long
heapSize
=
Runtime
.
getRuntime
().
totalMemory
();
log
.
info
(
"Current heap size{}m"
,
heapSize
/
1024
/
1024
);
//获取堆的最大大小byte单位
//超过将抛出 OutOfMemoryException
long
heapMaxSize
=
Runtime
.
getRuntime
().
maxMemory
();
log
.
info
(
"Gets the maximum heap size{}m"
,
heapMaxSize
/
1024
/
1024
);
//获取当前空闲的内存容量byte单位
long
heapFreeSize
=
Runtime
.
getRuntime
().
freeMemory
();
log
.
info
(
"Gets the current free memory capacity{}m"
,
heapFreeSize
/
1024
/
1024
);
System
.
gc
();
log
.
info
(
"gc-after"
);
heapSize
=
Runtime
.
getRuntime
().
totalMemory
();
log
.
info
(
"Current heap size{}m"
,
heapSize
/
1024
/
1024
);
//获取堆的最大大小byte单位
//超过将抛出 OutOfMemoryException
heapMaxSize
=
Runtime
.
getRuntime
().
maxMemory
();
log
.
info
(
"Gets the maximum heap size{}m"
,
heapMaxSize
/
1024
/
1024
);
//获取当前空闲的内存容量byte单位
heapFreeSize
=
Runtime
.
getRuntime
().
freeMemory
();
log
.
info
(
"Gets the current free memory capacity{}m"
,
heapFreeSize
/
1024
/
1024
);
System
.
gc
();
}
}
src/main/java/com/afanticar/aliyun/fc/HelloFC.java
0 → 100644
View file @
bb9af247
package
com
.
afanticar
.
aliyun
.
fc
;
import
cn.hutool.http.HttpUtil
;
import
com.afanticar.aliyun.fc.dto.request.SimpleRequest
;
import
com.afanticar.aliyun.fc.dto.response.SimpleResponse
;
import
com.afanticar.aliyun.fc.util.FFmpegUtil
;
import
com.aliyun.fc.runtime.Context
;
import
com.aliyun.fc.runtime.Credentials
;
import
com.aliyun.fc.runtime.PojoRequestHandler
;
import
com.aliyun.oss.OSS
;
import
com.aliyun.oss.OSSClientBuilder
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.File
;
import
java.util.Date
;
import
java.util.UUID
;
/**
* @Author: wuzhancai
* @Desc: hello
* @Date: 2023/6/2 13:55
*/
public
class
HelloFC
implements
PojoRequestHandler
<
SimpleRequest
,
SimpleResponse
>
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
HelloFC
.
class
);
private
static
String
ffmpegPath
=
"/code/ffmpeg"
;
static
{
try
{
String
result
=
FFmpegUtil
.
assignmentFfmpegAuthority
(
ffmpegPath
);
log
.
info
(
"result {}"
,
result
);
}
catch
(
Exception
e
){
log
.
error
(
"ffmpeg init fail {}"
,
e
);
}
}
@Override
public
SimpleResponse
handleRequest
(
SimpleRequest
request
,
Context
context
)
{
String
message
=
getAudio
(
request
,
context
);
return
new
SimpleResponse
(
message
);
}
public
static
void
main
(
String
[]
args
)
{
SimpleRequest
request
=
new
SimpleRequest
();
Date
beginDate
=
new
Date
();
log
.
info
(
"begin time {}"
,
beginDate
);
log
.
warn
(
"dfdfdf"
);
String
audio
=
getAudio
(
request
,
null
);
Date
endDate
=
new
Date
();
log
.
info
(
"end time {} user {} "
,
endDate
,(
endDate
.
getTime
()-
beginDate
.
getTime
()));
System
.
out
.
println
(
audio
);
}
private
static
String
getAudio
(
SimpleRequest
request
,
Context
context
)
{
String
message
=
"Hello, "
+
request
.
getFirstName
()
+
" "
+
request
.
getLastName
();
log
.
info
(
"helloo execute 111"
);
String
url
=
"https://api.amemv.com/aweme/v1/play/?video_id=v0200fg10000cgbkfqjc77u3udkfff30&line=1&file_id=db82e40c28ad49fb860a247ace95f8b8&sign=44b50f681b684101c3f33ce7c25ffd02&is_play_url=1&source=PackSourceEnum_PUBLISH"
;
String
folderPath
=
"/usr/aweme"
;
File
file
=
new
File
(
folderPath
);
if
(!
file
.
exists
()){
file
.
mkdirs
();
}
String
outputFilePath
=
folderPath
+
"/aa.wav"
;
// if(context != null){
// Bucket名称, 需要预先创建。
String
bucketName
=
"cont-aweme-dev"
;
// Endpoint必须填写Bucket所在地域对应的Endpoint,推荐使用内网访问地址。以华东1(杭州)为例,内网访问Endpoint为https://oss-cn-hangzhou-internal.aliyuncs.com。
String
endpoint
=
"https://oss-cn-hangzhou-internal.aliyuncs.com"
;
// String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 获取密钥信息,执行前,确保函数所在的服务配置了角色信息,并且角色需要拥有AliyunOSSFullAccess权限。
// 建议直接使用AliyunFCDefaultRole角色。
String
os
=
System
.
getProperty
(
"os.name"
);
if
(
os
.
toLowerCase
().
indexOf
(
"win"
)!=-
1
){
ffmpegPath
=
null
;
endpoint
=
"https://oss-cn-hangzhou.aliyuncs.com"
;
}
FFmpegUtil
.
live2Audio
(
ffmpegPath
,
url
,
"/code/temp/aa.wav"
,
null
,
null
);
log
.
info
(
"context {}"
,
context
);
Credentials
creds
=
null
;
if
(
context
!=
null
){
creds
=
context
.
getExecutionCredentials
();
log
.
info
(
"creds.getAccessKeyId{},creds.getAccessKeySecret{},creds.getSecurityToken{}"
,
creds
.
getAccessKeyId
(),
creds
.
getAccessKeySecret
(),
creds
.
getSecurityToken
());
}
OSS
ossClient
=
null
;
if
(
context
!=
null
){
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
creds
.
getAccessKeyId
(),
creds
.
getAccessKeySecret
(),
creds
.
getSecurityToken
());
}
else
{
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
"LTAI5tLJxsRWWNkT3tvCD4fS"
,
"0VETCYESe4J11ejEovggjnc5prrHq8"
);
}
// 填写Byte数组。
byte
[]
content
=
"Hello FC"
.
getBytes
();
// 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。
// ossClient.putObject(bucketName, "exampledir/"+UUID.randomUUID().toString()+".txt", new ByteArrayInputStream(content));
ossClient
.
putObject
(
bucketName
,
"exampledir/"
+
UUID
.
randomUUID
().
toString
()
+
".wav"
,
new
File
(
"/code/temp/aa.wav"
));
// 关闭OSSClient。
ossClient
.
shutdown
();
// outputStream.write(new String("done").getBytes());
String
body
=
"{\"ctime\":\"\",\"env\":\"\",\"equipmentId\":\"\",\"eventResult\":\"\",\"eventStep\":\"\",\"frameId\":\"\",\"platform\":\"\",\"roomId\":\"ff\",\"taskId\":\"aliyunfcc\",\"taskName\":\"\",\"time\":0}"
;
String
post
=
HttpUtil
.
post
(
"https://api-dev.afanticar.com/content-center/v1/kaize/publish/task"
,
body
);
log
.
info
(
"post {}"
,
post
);
//获取当前堆的大小 byte 单位
long
heapSize
=
Runtime
.
getRuntime
().
totalMemory
();
log
.
info
(
"Current heap size{}m"
,
heapSize
/
1024
/
1024
);
//获取堆的最大大小byte单位
//超过将抛出 OutOfMemoryException
long
heapMaxSize
=
Runtime
.
getRuntime
().
maxMemory
();
log
.
info
(
"Gets the maximum heap size{}m"
,
heapMaxSize
/
1024
/
1024
);
//获取当前空闲的内存容量byte单位
long
heapFreeSize
=
Runtime
.
getRuntime
().
freeMemory
();
log
.
info
(
"Gets the current free memory capacity{}m"
,
heapFreeSize
/
1024
/
1024
);
System
.
gc
();
log
.
info
(
"gc-after"
);
heapSize
=
Runtime
.
getRuntime
().
totalMemory
();
log
.
info
(
"Current heap size{}m"
,
heapSize
/
1024
/
1024
);
//获取堆的最大大小byte单位
//超过将抛出 OutOfMemoryException
heapMaxSize
=
Runtime
.
getRuntime
().
maxMemory
();
log
.
info
(
"Gets the maximum heap size{}m"
,
heapMaxSize
/
1024
/
1024
);
//获取当前空闲的内存容量byte单位
heapFreeSize
=
Runtime
.
getRuntime
().
freeMemory
();
log
.
info
(
"Gets the current free memory capacity{}m"
,
heapFreeSize
/
1024
/
1024
);
System
.
gc
();
return
message
;
}
}
src/main/java/com/afanticar/aliyun/fc/dto/request/AwemeSimpleRequest.java
0 → 100644
View file @
bb9af247
package
com
.
afanticar
.
aliyun
.
fc
.
dto
.
request
;
import
java.util.Date
;
/**
* @Author: wuzhancai
* @Desc:
* @Date: 2023-06-07 17:10
*/
public
class
AwemeSimpleRequest
{
String
bucketName
;
String
authorId
;
String
platform
;
String
awemeId
;
String
awemeUrl
;
String
contentCenterUrl
;
Date
publishTime
;
public
String
getAwemeUrl
()
{
return
awemeUrl
;
}
public
void
setAwemeUrl
(
String
awemeUrl
)
{
this
.
awemeUrl
=
awemeUrl
;
}
public
String
getContentCenterUrl
()
{
return
contentCenterUrl
;
}
public
void
setContentCenterUrl
(
String
contentCenterUrl
)
{
this
.
contentCenterUrl
=
contentCenterUrl
;
}
public
String
getAwemeId
()
{
return
awemeId
;
}
public
void
setAwemeId
(
String
awemeId
)
{
this
.
awemeId
=
awemeId
;
}
public
String
getAuthorId
()
{
return
authorId
;
}
public
void
setAuthorId
(
String
authorId
)
{
this
.
authorId
=
authorId
;
}
public
String
getPlatform
()
{
return
platform
;
}
public
void
setPlatform
(
String
platform
)
{
this
.
platform
=
platform
;
}
public
Date
getPublishTime
()
{
return
publishTime
;
}
public
void
setPublishTime
(
Date
publishTime
)
{
this
.
publishTime
=
publishTime
;
}
public
String
getBucketName
()
{
return
bucketName
;
}
public
void
setBucketName
(
String
bucketName
)
{
this
.
bucketName
=
bucketName
;
}
}
src/main/java/com/afanticar/aliyun/fc/dto/request/SimpleRequest.java
0 → 100644
View file @
bb9af247
package
com
.
afanticar
.
aliyun
.
fc
.
dto
.
request
;
/**
* @Author: wuzhancai
* @Desc:
* @Date: 2023/6/2 13:52
*/
public
class
SimpleRequest
{
String
firstName
;
String
lastName
;
public
String
getFirstName
()
{
return
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
public
String
getLastName
()
{
return
lastName
;
}
public
void
setLastName
(
String
lastName
)
{
this
.
lastName
=
lastName
;
}
public
SimpleRequest
()
{}
public
SimpleRequest
(
String
firstName
,
String
lastName
)
{
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
}
}
src/main/java/com/afanticar/aliyun/fc/dto/response/AwemeCallbackContent.java
0 → 100644
View file @
bb9af247
package
com
.
afanticar
.
aliyun
.
fc
.
dto
.
response
;
import
java.util.Date
;
import
java.util.List
;
/**
* @Author: wuzhancai
* @Desc: 短视频回调给内容中心
* @Date: 2023/6/7 17:27
*/
public
class
AwemeCallbackContent
{
String
authorId
;
String
platform
;
String
awemeId
;
String
awemeAudioUrl
;
List
<
String
>
awemeFrames
;
Date
publishTime
;
public
String
getAwemeAudioUrl
()
{
return
awemeAudioUrl
;
}
public
void
setAwemeAudioUrl
(
String
awemeAudioUrl
)
{
this
.
awemeAudioUrl
=
awemeAudioUrl
;
}
public
List
<
String
>
getAwemeFrames
()
{
return
awemeFrames
;
}
public
void
setAwemeFrames
(
List
<
String
>
awemeFrames
)
{
this
.
awemeFrames
=
awemeFrames
;
}
public
String
getAwemeId
()
{
return
awemeId
;
}
public
void
setAwemeId
(
String
awemeId
)
{
this
.
awemeId
=
awemeId
;
}
public
String
getAuthorId
()
{
return
authorId
;
}
public
void
setAuthorId
(
String
authorId
)
{
this
.
authorId
=
authorId
;
}
public
String
getPlatform
()
{
return
platform
;
}
public
void
setPlatform
(
String
platform
)
{
this
.
platform
=
platform
;
}
public
Date
getPublishTime
()
{
return
publishTime
;
}
public
void
setPublishTime
(
Date
publishTime
)
{
this
.
publishTime
=
publishTime
;
}
}
src/main/java/com/afanticar/aliyun/fc/dto/response/SimpleResponse.java
0 → 100644
View file @
bb9af247
package
com
.
afanticar
.
aliyun
.
fc
.
dto
.
response
;
/**
* @Author: wuzhancai
* @Desc:
* @Date: 2023/6/2 13:54
*/
public
class
SimpleResponse
{
String
message
;
public
String
getMessage
()
{
return
message
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
public
SimpleResponse
()
{}
public
SimpleResponse
(
String
message
)
{
this
.
message
=
message
;
}
}
src/main/java/com/afanticar/aliyun/fc/util/FFmpegUtil.java
0 → 100644
View file @
bb9af247
package
com
.
afanticar
.
aliyun
.
fc
.
util
;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.afanticar.aliyun.fc.HelloFC
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.*
;
import
java.util.*
;
import
java.util.concurrent.TimeUnit
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* @author :wuzhancai
* @date :2021-9-27 17:35
* @description:ffmpeg工具集
*/
public
class
FFmpegUtil
{
private
final
static
Integer
SUCCESS
=
0
;
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
FFmpegUtil
.
class
);
//进行抽帧图片
public
static
String
getFramesByPic
(
String
ffmpegPath
,
String
proxyIp
,
String
timeout
,
String
liveUrl
,
String
savePath
,
String
fps
,
String
startTime
,
String
endTime
)
{
//前10秒 500ms抽一帧 后剩下10s 2s抽1帧
// Stopwatch stopwatch = Stopwatch.createStarted();
List
<
String
>
command
=
new
ArrayList
<>();
//获取操作系统名称
String
os
=
System
.
getProperty
(
"os.name"
);
command
.
add
(
ffmpegPath
);
if
(
StrUtil
.
isNotBlank
(
proxyIp
))
{
command
.
add
(
"-http_proxy"
);
command
.
add
(
"http://"
+
proxyIp
);
}
command
.
add
(
"-i"
);
command
.
add
(
liveUrl
);
if
(
StrUtil
.
isNotEmpty
(
startTime
))
{
command
.
add
(
"-ss"
);
command
.
add
(
startTime
);
}
if
(
StrUtil
.
isNotEmpty
(
endTime
))
{
command
.
add
(
"-t"
);
command
.
add
(
endTime
);
}
command
.
add
(
"-q"
);
command
.
add
(
"30"
);
command
.
add
(
"-vf"
);
command
.
add
(
"fps="
+
fps
);
command
.
add
(
"-qscale:v"
);
command
.
add
(
"2"
);
command
.
add
(
savePath
+
"/%05d.jpeg"
);
String
str
=
executeCommand
(
command
);
log
.
info
(
"抽帧视频地址{}所用时长{}ffmpeg返回的字符串{}"
,
liveUrl
,
null
,
str
);
return
str
;
}
/**
* 抽帧视频-根据时间抽一张
*
* @param ffmpegPath
* @param liveUrl
* @param time
* @param savePath
* @param fileName
* @return
*/
public
static
String
getOneFrame
(
String
ffmpegPath
,
String
liveUrl
,
String
time
,
String
savePath
,
String
fileName
)
{
// Stopwatch stopwatch = Stopwatch.createStarted();
List
<
String
>
command
=
new
ArrayList
<>();
command
.
add
(
ffmpegPath
);
command
.
add
(
"-ss"
);
command
.
add
(
time
);
command
.
add
(
"-i"
);
command
.
add
(
liveUrl
);
command
.
add
(
"-vframes"
);
command
.
add
(
"1"
);
command
.
add
(
savePath
+
fileName
);
String
str
=
executeCommand
(
command
);
log
.
info
(
"抽帧一张图片{}所用时长{}ffmpeg返回的字符串{}"
,
liveUrl
,
null
,
str
);
return
str
;
}
/**
* 注册ffmpeg权限
* @param ffmpegPath
* @return
*/
public
static
String
assignmentFfmpegAuthority
(
String
ffmpegPath
)
{
//前10秒 500ms抽一帧 后剩下10s 2s抽1帧
// Stopwatch stopwatch = Stopwatch.createStarted();
List
<
String
>
command
=
new
ArrayList
<>();
String
str
=
""
;
if
(
StrUtil
.
isNotBlank
(
ffmpegPath
)){
command
.
add
(
"chmod"
);
command
.
add
(
"+x"
);
command
.
add
(
ffmpegPath
);
str
=
executeCommand
(
command
);
log
.
info
(
"ffmpeg赋值权限{},{}"
,
ffmpegPath
,
str
);
}
return
str
;
}
public
static
String
live2Audio
(
String
ffmpegPath
,
String
liveUrl
,
String
savePath
,
String
startTime
,
String
endTime
)
{
createFolder
(
savePath
);
// 构建命令
List
<
String
>
command
=
new
ArrayList
();
if
(
StrUtil
.
isNotBlank
(
ffmpegPath
))
{
command
.
add
(
ffmpegPath
);
}
else
{
command
.
add
(
"ffmpeg"
);
}
command
.
add
(
"-y"
);
//时间
command
.
add
(
"-i"
);
command
.
add
(
liveUrl
);
if
(
StrUtil
.
isNotBlank
(
startTime
))
{
command
.
add
(
"-ss"
);
command
.
add
(
startTime
);
}
if
(
StrUtil
.
isNotBlank
(
endTime
))
{
command
.
add
(
"-t"
);
command
.
add
(
endTime
);
}
command
.
add
(
"-ar"
);
command
.
add
(
"16000"
);
command
.
add
(
"-ac"
);
command
.
add
(
"1"
);
command
.
add
(
"-ab"
);
command
.
add
(
"256K"
);
command
.
add
(
savePath
);
String
executeResult
=
executeCommand
(
command
);
return
executeResult
;
}
public
static
void
createFolder
(
String
filepath
)
{
//如果文件夹不存在则创建
File
file
=
new
File
(
filepath
);
File
fileParent
=
file
.
getParentFile
();
if
(!
fileParent
.
exists
())
{
fileParent
.
mkdirs
();
}
}
private
static
int
getTimelen
(
String
timelen
)
{
int
min
=
0
;
String
strs
[]
=
timelen
.
split
(
":"
);
if
(
strs
[
0
].
compareTo
(
"0"
)
>
0
)
{
// 秒
min
+=
Integer
.
valueOf
(
strs
[
0
])
*
60
*
60
;
}
if
(
strs
[
1
].
compareTo
(
"0"
)
>
0
)
{
min
+=
Integer
.
valueOf
(
strs
[
1
])
*
60
;
}
if
(
strs
[
2
].
compareTo
(
"0"
)
>
0
)
{
min
+=
Math
.
round
(
Float
.
valueOf
(
strs
[
2
]));
}
return
min
;
}
/**
* 执行FFmpeg命令
*
* @param commands 要执行的FFmpeg命令
* @return FFmpeg程序在执行命令过程中产生的各信息,执行出错时返回null
*/
public
static
String
executeCommand
(
List
<
String
>
commands
)
{
long
beginDate
=
System
.
currentTimeMillis
();
if
(
CollUtil
.
isEmpty
(
commands
))
{
log
.
error
(
"--- 指令执行失败,因为要执行的FFmpeg指令为空! ---"
);
return
null
;
}
LinkedList
<
String
>
ffmpegCmds
=
new
LinkedList
<>(
commands
);
log
.
info
(
"--- 待执行的FFmpeg指令为:--- {}"
+
ffmpegCmds
);
String
cmdStr
=
Arrays
.
toString
(
ffmpegCmds
.
toArray
()).
replace
(
","
,
""
);
log
.
info
(
"--- 正在执行的FFmpeg指令为:--- {}"
,
cmdStr
);
Runtime
runtime
=
Runtime
.
getRuntime
();
Process
ffmpeg
=
null
;
// Stopwatch stopwatch = Stopwatch.createUnstarted();
InputStream
errorStream1
=
null
;
InputStream
inputStream1
=
null
;
try
{
// 创建stopwatch并开始计时
// stopwatch.start();
// 执行ffmpeg指令
ProcessBuilder
builder
=
new
ProcessBuilder
();
builder
.
command
(
ffmpegCmds
);
builder
.
redirectErrorStream
(
false
);
ffmpeg
=
builder
.
start
();
log
.
info
(
"--- 开始执行FFmpeg指令:--- 执行线程名:"
+
builder
.
toString
());
// 取出输出流和错误流的信息
// 注意:必须要取出ffmpeg在执行命令过程中产生的输出信息,如果不取的话当输出流信息填满jvm存储输出留信息的缓冲区时,线程就回阻塞住
errorStream1
=
ffmpeg
.
getErrorStream
();
inputStream1
=
ffmpeg
.
getInputStream
();
PrintStream
errorStream
=
new
PrintStream
(
errorStream1
);
PrintStream
inputStream
=
new
PrintStream
(
inputStream1
);
errorStream
.
start
();
inputStream
.
start
();
// 等待ffmpeg命令执行完
int
state
=
ffmpeg
.
waitFor
();
// 获取执行结果字符串
String
result
=
errorStream
.
stringBuffer
.
append
(
inputStream
.
stringBuffer
).
toString
();
if
(
state
!=
SUCCESS
||
result
.
indexOf
(
"nothing was encoded"
)
!=
-
1
||
result
.
indexOf
(
"Input/output error"
)
!=
-
1
)
{
log
.
info
(
"--- 已执行的FFmepg命令: ---"
+
cmdStr
+
" 已执行完毕,执行结果:【异常】, {}"
,
result
);
return
result
;
}
log
.
info
(
"--- FFmepg命令已执行完毕: ---"
+
cmdStr
+
" 已执行完毕,执行结果:{}"
,
errorStream
.
stringBuffer
.
append
(
inputStream
.
stringBuffer
).
toString
());
return
null
;
}
catch
(
Exception
e
)
{
log
.
error
(
"--- FFmpeg命令执行出错! --- 出错信息: {}"
+
e
);
throw
new
RuntimeException
(
e
.
getMessage
());
}
finally
{
if
(
null
!=
ffmpeg
&&
ffmpeg
.
isAlive
())
{
ffmpeg
.
destroy
();
}
long
endDate
=
System
.
currentTimeMillis
();
// stopwatch.stop();
log
.
info
(
"--- FFmepg命令执行时长: ---"
+
(
endDate
-
beginDate
)
+
"毫秒"
);
}
}
/**
* 用于取出ffmpeg线程执行过程中产生的各种输出和错误流的信息
*/
static
class
PrintStream
extends
Thread
{
InputStream
inputStream
=
null
;
BufferedReader
bufferedReader
=
null
;
StringBuffer
stringBuffer
=
new
StringBuffer
();
public
PrintStream
(
InputStream
inputStream
)
{
this
.
inputStream
=
inputStream
;
}
@Override
public
void
run
()
{
try
{
if
(
null
==
inputStream
)
{
log
.
error
(
"--- 读取输出流出错!因为当前输出流为空!---"
);
}
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
));
String
line
=
null
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
log
.
debug
(
line
);
stringBuffer
.
append
(
line
+
"*****"
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"--- 读取输入流出错了!--- 错误信息:"
+
e
.
getMessage
());
}
finally
{
try
{
if
(
null
!=
bufferedReader
)
{
bufferedReader
.
close
();
}
if
(
null
!=
inputStream
)
{
inputStream
.
close
();
}
}
catch
(
IOException
e
)
{
log
.
error
(
"--- 调用PrintStream读取输出流后,关闭流时出错!---"
);
}
}
}
}
/**
* 在程序退出前结束已有的FFmpeg进程
*/
private
static
class
ProcessKiller
extends
Thread
{
private
Process
process
;
public
ProcessKiller
(
Process
process
)
{
this
.
process
=
process
;
}
@Override
public
void
run
()
{
this
.
process
.
destroy
();
log
.
info
(
"--- 已销毁FFmpeg进程 --- 进程名: "
+
process
.
toString
());
}
}
public
static
void
main
(
String
[]
args
)
{
//测试获取格式类型
// String ffmpegUrl = "D:\\github\\ffmpeg-N-102781-g05f9b3a0a5-win64-gpl\\bin\\ffmpeg.exe";
// String liveUrl = "http://pull-flv-l6.douyincdn.com/stage/stream-686841983849463839.flv";
// String videoType = getVideoType(ffmpegUrl, liveUrl);
// System.out.println("视频格式为->" + videoType);
// String awemeUrl = "https://api-hl.amemv.com/aweme/v1/play/?video_id=v0200fg10000c7qvfr3c77u9gfo5fds0&line=1&file_id=2e39733bf21545fc80c8eea6d42c3add&sign=9cfdfd380af8105671a8ebfca654209e&is_play_url=1&source=PackSourceEnum_PUBLISH";
// int videoTime = getVideoTime(ffmpegUrl, awemeUrl);
// System.out.println("视频时长为->" + videoTime);
// liveUrl = "http://tx.pull.yximgs.com/live/6000_l7abfpSdG4U.flv?txSecret=1621317c6ceba238af569b85ceccc4b6&txTime=617cded1&stat=WT%2BavlGMAdsjOUeSy7esaXb44bKwlLFe%2FYytqjx3K47iVB261DYdo7OK3aYXLoFE&fd=1&ss=s1";
// videoType = getVideoType(ffmpegUrl, liveUrl);
// System.out.println("视频格式为->" + videoType);
//
// liveUrl = "http://ali-origin.pull.yximgs.com/gifshow/m5tJoLjvsmQ.flv?auth_key=1635573443-0-0-7d6e730acd6b8b0d59b75ac62bfe3f94&oidc=edge_hb&fd=1&ss=s19";
// videoType = getVideoType(ffmpegUrl, liveUrl);
// System.out.println("视频格式为->" + videoType);
// String framesByPic = getFramesByPic(ffmpegUrl, awemeUrl, "d://test", "1/5", null, null);
String
str
=
"Output file is empty, nothInput/output errorng was encoded"
;
System
.
out
.
println
(
str
.
indexOf
(
"Input/output error"
));
System
.
out
.
println
(
System
.
getProperty
(
"os.name"
));
}
}
src/main/resources/ffmpeg
0 → 100644
View file @
bb9af247
File added
src/main/resources/log4j.properties
0 → 100644
View file @
bb9af247
### ????????dubug????????stdout,D
log4j.rootLogger
=
debug,stdout,HelloFC
### ???????? ###
log4j.appender.stdout
=
org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target
=
System.out
log4j.appender.debug
=
org.apache.log4j.RollingFileAppender
#?????????
log4j.appender.debug.MaxFileSize
=
5M
#?????????
log4j.appender.stdout.layout
=
org.apache.log4j.PatternLayout
#?????????????c??printf?%d?%m???????????????
log4j.appender.stdout.layout.ConversionPattern
=
[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### ??warn ???????????
# ??????D:/logs/error.log4j
#log4j.appender.D = org.apache.log4j.FileAppender
#log4j.appender.D.File = /usr/logs/error.log
#log4j.appender.D.Append = true
#log4j.appender.D.Threshold = warn
#log4j.appender.D.layout = org.apache.log4j.PatternLayout
#log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.HelloFC
=
org.apache.log4j.FileAppender
log4j.appender.HelloFC.File
=
/usr/logs/error.log
log4j.appender.HelloFC.Append
=
true
#log4j.appender.HelloFC.Threshold = ERROR
log4j.appender.HelloFC.Threshold
=
INFO
log4j.appender.HelloFC.layout
=
org.apache.log4j.PatternLayout
log4j.appender.HelloFC.layout.ConversionPattern
=
%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment