Skip to content

Commit

Permalink
bug: 修复视频转录过程中进程挂了 TencentBlueKing#2775
Browse files Browse the repository at this point in the history
  • Loading branch information
felixncheng committed Nov 20, 2024
1 parent 25d52a8 commit d84ce74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class Mux {
* @param inputStream 视频输入流
* @param output 封装后的文件
* */
constructor(inputStream: InputStream, output: File) : this() {
constructor(inputStream: InputStream, output: File, name: String) : this() {
this.inputStream = inputStream
this.outputFile = output
this.fileName = inputStream.toString()
this.fileName = name
}

constructor()
Expand All @@ -55,6 +55,9 @@ class Mux {
private var stopFlag: AtomicBoolean = AtomicBoolean(false)
private var writeHeader = false

@Volatile
var packetCount = 0

fun start() {
if (!running.compareAndSet(false, true)) {
logger.info("Mux was already started")
Expand Down Expand Up @@ -129,7 +132,6 @@ class Mux {
}
writeHeader = true
var dts = 0L
var count = 0L
while (!stopFlag.get()) {
if (avformat.av_read_frame(ifmtCtx, pkt) < 0) {
break
Expand All @@ -151,16 +153,17 @@ class Mux {
pkt!!.pos(-1)
logPacket(ofmtCtx, pkt!!, "out")
check(avformat.av_interleaved_write_frame(ofmtCtx, pkt) >= 0) { "write frame error" }
count++
packetCount++
}
logger.info("Complete remux $fileName,size ${outputFile!!.length()} B,$count packet.")
logger.info("Complete remux $fileName,size ${outputFile!!.length()} B,$packetCount packet.")
} catch (e: Exception) {
logger.error("Remux error:", e)
throw e
} finally {
release()
scope.close()
running.set(false)
logger.info("Finish remux $fileName to ${outputFile!!.absolutePath}")
}
}

Expand Down Expand Up @@ -200,9 +203,11 @@ class Mux {
if (writeHeader) {
avformat.av_write_trailer(ofmtCtx)
}
logger.info("Finish remux $fileName to ${outputFile!!.absolutePath}")
if (ofmtCtx!!.oformat().flags() and avformat.AVFMT_NOFILE == 0) {
avformat.avio_closep(ofmtCtx!!.pb())
val oformat = ofmtCtx!!.oformat()
if (oformat != null) {
if (oformat.flags() and avformat.AVFMT_NOFILE == 0 && !ofmtCtx!!.pb().isNull) {
avformat.avio_closep(ofmtCtx!!.pb())
}
}
avformat.avformat_free_context(ofmtCtx)
ofmtCtx = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RemuxRecordingListener(
fileName = "$name.$fileType"
val tempFileName = StringPool.randomStringByLongValue(REMUX_PREFIX, ".$fileType")
tempFilePath = Paths.get(path, tempFileName)
mux = Mux(pipeIn, tempFilePath!!.toFile())
mux = Mux(pipeIn, tempFilePath!!.toFile(), name)
val remuxFuture = threadPool.submit {
try {
mux!!.start()
Expand All @@ -90,7 +90,11 @@ class RemuxRecordingListener(
pipeOut.close()
mux!!.stop()
pipeIn.close()
fileConsumer.accept(tempFilePath!!.toFile(), fileName!!)
if (mux!!.packetCount > 0) {
fileConsumer.accept(tempFilePath!!.toFile(), fileName!!)
} else {
logger.warn("empty stream $fileName")
}
}
} finally {
Files.deleteIfExists(tempFilePath)
Expand Down

0 comments on commit d84ce74

Please sign in to comment.