Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
W
wasm-trans-mp3
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
yang.jie
wasm-trans-mp3
Commits
fdc9f791
Commit
fdc9f791
authored
Mar 18, 2022
by
yang.jie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add flush encoder
parent
cdf5808f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
101 additions
and
44 deletions
+101
-44
build-demo.sh
build-demo.sh
+1
-1
CMakeLists.txt
demo_transcode_mp3/CMakeLists.txt
+12
-6
FFAudioDecoder.cpp
demo_transcode_mp3/audio/FFAudioDecoder.cpp
+6
-5
FFAudioEncoder.cpp
demo_transcode_mp3/audio/FFAudioEncoder.cpp
+74
-29
FFAudioEncoder.h
demo_transcode_mp3/audio/FFAudioEncoder.h
+8
-3
No files found.
build-demo.sh
View file @
fdc9f791
...
...
@@ -10,7 +10,7 @@ echo "WORKPATH"=$WORKPATH
rm
-rf
${
WORKPATH
}
/demo/mp4encoder.js
${
WORKPATH
}
/demo/mp4encoder.wasm
FFMPEG_ST
=
yes
FFMPEG_ST
=
no
EMSDK
=
/emsdk
...
...
demo_transcode_mp3/CMakeLists.txt
View file @
fdc9f791
...
...
@@ -2,6 +2,14 @@ cmake_minimum_required(VERSION 3.10.0)
project
(
transcode_mp3
)
set
(
CMAKE_CXX_STANDARD 17
)
set
(
CMAKE_CXX_FLAGS
"\
-pthread \
-Wno-deprecated-declarations \
-Wno-pointer-sign \
-Wno-implicit-int-float-conversion \
-Wno-switch -Wno-parentheses\
-Qunused-arguments \
"
)
# set(CMAKE_EXECUTABLE_SUFFIX ".html") # 编译生成.html
set
(
CMAKE_VERBOSE_MAKEFILE on
)
...
...
@@ -24,17 +32,11 @@ add_subdirectory(audio)
add_subdirectory
(
ffmbase
)
set_target_properties
(
transcode_mp3 PROPERTIES LINK_FLAGS
"\
-Wno-deprecated-declarations \
-Wno-pointer-sign \
-Wno-implicit-int-float-conversion \
-Wno-switch -Wno-parentheses\
-Qunused-arguments \
-s FORCE_FILESYSTEM=1 \
-s WASM=1 \
-s USE_SDL=2 \
-s INVOKE_RUN=0 \
-s EXIT_RUNTIME=1 \
-s MODULARIZE=1 \
-s EXPORT_NAME=
\"
createTrancodeMp3
\"
\
-s EXPORTED_FUNCTIONS=
\"
[_main,_malloc,_free]
\"
\
-s EXPORTED_RUNTIME_METHODS=
\"
[FS, cwrap, ccall, setValue, writeAsciiToMemory]
\"
\
...
...
@@ -42,6 +44,10 @@ set_target_properties(transcode_mp3 PROPERTIES LINK_FLAGS "\
-s ALLOW_MEMORY_GROWTH=1 \
--pre-js
${
PRE_PATH
}
/pre.js \
--post-js
${
POST_PATH
}
/post.js \
-pthread \
-s USE_PTHREADS=1 \
-s PROXY_TO_PTHREAD=1 \
-s PTHREAD_POOL_SIZE=8\
"
)
target_link_libraries
(
${
PROJECT_NAME
}
PRIVATE audio
)
...
...
demo_transcode_mp3/audio/FFAudioDecoder.cpp
View file @
fdc9f791
...
...
@@ -56,11 +56,8 @@ namespace FFM {
void
FFAudioDecoder
::
startDecoder
()
{
// std::thread t([this]() {
// decodeAudio();
// });
// t.join();
decodeAudio
();
}
int
FFAudioDecoder
::
decode
(
int
*
gotframe
)
...
...
@@ -168,10 +165,14 @@ namespace FFM {
//}
}
}
av_packet_unref
(
m_pPkt
);
}
if
(
m_audioEncoder
)
{
m_audioEncoder
->
flush_encoder
();
}
printf
(
"------------------count=%d
\n
"
,
count
);
/*if (m_audioEncoder) {
m_audioEncoder->closeEncoder();
...
...
demo_transcode_mp3/audio/FFAudioEncoder.cpp
View file @
fdc9f791
...
...
@@ -834,12 +834,47 @@ namespace FFM {
printf
(
"File duration: %02d:%02d:%02d
\n
"
,
thh
,
tmm
,
tss
);
}
int
FFAudioEncoder
::
writePacket
(
AVPacket
*
pkt
)
{
int
ret
=
-
1
;
av_packet_rescale_ts
(
pkt
,
m_pCodecCtx
->
time_base
,
m_pStream
->
time_base
);
pkt
->
stream_index
=
m_pStream
->
index
;
//AVStream *in_stream = m_pInStream;
////pkt.pts = av_rescale_q(m_swr->getLastTime(), m_swr->getTimeBase(), m_pFormatCtx->streams[pkt.stream_index]->time_base);
////
////pkt.pts = av_frame_get_best_effort_timestamp(filter_frame);
////pkt.dts = pkt.pts;
////pkt.pts = pkt.dts = m_frameIndex++ *( m_pCodecCtx->frame_size * 1000 / m_pCodecCtx->sample_rate);
pkt
->
pts
=
av_rescale_q_rnd
(
pkt
->
pts
,
m_pInCodeCtx
->
time_base
,
m_pStream
->
time_base
,
(
AVRounding
)(
AV_ROUND_NEAR_INF
|
AV_ROUND_PASS_MINMAX
));
pkt
->
dts
=
av_rescale_q_rnd
(
pkt
->
dts
,
m_pInCodeCtx
->
time_base
,
m_pStream
->
time_base
,
(
AVRounding
)(
AV_ROUND_NEAR_INF
|
AV_ROUND_PASS_MINMAX
));
pkt
->
duration
=
av_rescale_q
(
pkt
->
duration
,
m_pInCodeCtx
->
time_base
,
m_pStream
->
time_base
);
//pkt.pos = -1;
printf
(
"timestamp: %lf
\n
"
,
pkt
->
pts
*
av_q2d
(
m_pStream
->
time_base
));
printf
(
"pkt.size: %d, frame.nb_samples: %d
\n
pts: %ld, dts: %ld, duration: %ld
\n
"
,
pkt
->
size
,
\
m_pStream
->
codec
->
frame_size
,
\
pkt
->
pts
,
pkt
->
dts
,
pkt
->
duration
);
ret
=
av_interleaved_write_frame
(
m_pFormatCtx
,
pkt
);
return
ret
;
}
int
FFAudioEncoder
::
encodeAudio
(
AVCodecContext
*
inCtx
,
AVFrame
*
frame
,
uint64_t
starttime
)
{
uint64_t
time
=
av_gettime
()
-
starttime
;
int
ret
=
-
1
;
if
(
!
m_pFormatCtx
)
return
-
1
;
if
(
!
m_pFormatCtx
||
!
inCtx
)
return
-
1
;
m_pInCodeCtx
=
inCtx
;
AVFrame
*
outframe
=
nullptr
;
...
...
@@ -886,39 +921,15 @@ namespace FFM {
}
AVPacket
pkt
;
av_init_packet
(
&
pkt
);
pkt
.
data
=
nullptr
;
pkt
.
size
=
0
;
av_init_packet
(
&
pkt
);
while
(
avcodec_receive_packet
(
m_pCodecCtx
,
&
pkt
)
>=
0
)
{
printf
(
"encoding.
\n
"
);
av_packet_rescale_ts
(
&
pkt
,
m_pCodecCtx
->
time_base
,
m_pStream
->
time_base
);
pkt
.
stream_index
=
m_pStream
->
index
;
//AVStream *in_stream = m_pInStream;
////pkt.pts = av_rescale_q(m_swr->getLastTime(), m_swr->getTimeBase(), m_pFormatCtx->streams[pkt.stream_index]->time_base);
////
////pkt.pts = av_frame_get_best_effort_timestamp(filter_frame);
////pkt.dts = pkt.pts;
////pkt.pts = pkt.dts = m_frameIndex++ *( m_pCodecCtx->frame_size * 1000 / m_pCodecCtx->sample_rate);
pkt
.
pts
=
av_rescale_q_rnd
(
pkt
.
pts
,
inCtx
->
time_base
,
m_pStream
->
time_base
,
(
AVRounding
)(
AV_ROUND_NEAR_INF
|
AV_ROUND_PASS_MINMAX
));
pkt
.
dts
=
av_rescale_q_rnd
(
pkt
.
dts
,
inCtx
->
time_base
,
m_pStream
->
time_base
,
(
AVRounding
)(
AV_ROUND_NEAR_INF
|
AV_ROUND_PASS_MINMAX
));
pkt
.
duration
=
av_rescale_q
(
pkt
.
duration
,
inCtx
->
time_base
,
m_pStream
->
time_base
);
pkt
.
pos
=
-
1
;
printf
(
"timestamp: %lf
\n
"
,
pkt
.
pts
*
av_q2d
(
m_pStream
->
time_base
));
printf
(
"pkt.size: %d, frame.nb_samples: %d
\n
pts: %ld, dts: %ld, duration: %ld
\n
"
,
pkt
.
size
,
m_pStream
->
codec
->
frame_size
,
pkt
.
pts
,
pkt
.
dts
,
pkt
.
duration
);
m_pStream
->
duration
=
m_pInStream
->
duration
;
av_interleaved_write_frame
(
m_pFormatCtx
,
&
pkt
);
writePacket
(
&
pkt
);
av_packet_unref
(
&
pkt
);
}
...
...
@@ -937,4 +948,38 @@ namespace FFM {
}
while
(
r
);
return
ret
;
}
}
int
FFAudioEncoder
::
flush_encoder
()
{
int
ret
=
-
1
;
for
(;;)
{
AVPacket
pkt
;
av_init_packet
(
&
pkt
);
pkt
.
data
=
nullptr
;
pkt
.
size
=
0
;
while
((
ret
=
avcodec_receive_packet
(
m_pCodecCtx
,
&
pkt
))
==
AVERROR
(
EAGAIN
))
{
ret
=
avcodec_send_frame
(
m_pCodecCtx
,
NULL
);
if
(
ret
<
0
)
{
printf
(
"encoding failed!
\n
"
);
exit
(
1
);
}
printf
(
">>> have encode pkt
\n
"
);
}
if
(
ret
==
AVERROR_EOF
||
ret
<
0
)
{
//av_packet_unref(&pkt);
break
;
}
writePacket
(
&
pkt
);
av_packet_unref
(
&
pkt
);
}
printf
(
">>>> exit flush
\n
"
);
return
ret
;
}
}
\ No newline at end of file
demo_transcode_mp3/audio/FFAudioEncoder.h
View file @
fdc9f791
...
...
@@ -5,7 +5,7 @@
/*
.ogg DE y
.opus E
n
.opus E
y
.aac DE y
.mp3 DE y
.flac DE y
...
...
@@ -14,9 +14,9 @@
.m4a D y
.oga E y
.mid n
.webm DE
n
.webm DE
y
.weba n
.amr DE
n
.amr DE
y
.au DE y
.wma D y
.aiff DE y
...
...
@@ -180,12 +180,17 @@ namespace FFM
m_filter
=
fileter
;
}
int
flush_encoder
();
private
:
int
openFile
(
std
::
string
&
fileName
);
int
openCodec
();
int
writePacket
(
AVPacket
*
pkt
);
private
:
AVStream
*
m_pInStream
=
nullptr
;
AVCodecContext
*
m_pInCodeCtx
=
nullptr
;
AVFormatContext
*
m_pFormatCtx
=
nullptr
;
AVOutputFormat
*
m_pOutFmt
=
nullptr
;
...
...
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