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
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
888 additions
and
0 deletions
+888
-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
+0
-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
This diff is collapsed.
Click to expand it.
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