gpt4 book ai didi

node.js - JSON 中视频长度位置 0 处出现意外标记 G

转载 作者:太空宇宙 更新时间:2023-11-04 03:13:22 24 4
gpt4 key购买 nike

我正在使用video-length在nodejs中获取视频的持续时间。该代码在 Centos 中运行良好。

在 Ubuntu 16.x 中显示错误:

SyntaxError: Unexpected token G in JSON at position 0
at JSON.parse (<anonymous>)
at VideoLength (/.../node_modules/video-length/video-length.js:14:24)

我使用下面的代码来获取持续时间:

const VideoLength = require('video-length');    
VideoLength(video, {
bin: '/usr/bin/mediainfo',
extended: true
})
.then(data => {
console.log("data: %j", data)
duration = data['duration']
console.log("duration: " + duration)
})
.catch(err => {
console.log(err);
})

我也安装了mediainfo。如果我做错了什么,请指导我。

调试 video-length.js 后,我发现 Centos 和 Ubuntu 中获得的标准输出存在以下差异。在 Centos 中:

{
"media": {
"@ref": "/var/kurento/tmp/30/27122019/1514kurento-recording.webm",
"track": [
{
"@type": "General",
"VideoCount": "1",
"FileExtension": "webm",
"Format": "WebM",
"Format_Version": "2",
"FileSize": "157078809",
"FrameRate": "25.028",
"IsStreamable": "Yes",
"Encoded_Date": "UTC 2019-12-27 09:44:33",
"File_Modified_Date": "UTC 2019-12-27 10:56:31",
"File_Modified_Date_Local": "2019-12-27 16:26:31",
"Encoded_Application": "GStreamer Matroska muxer",
"Encoded_Library": "GStreamer matroskamux version 1.8.1.1",
"extra": {
"IsTruncated": "Yes"
}
},
{
"@type": "Video",
"StreamOrder": "0",
"ID": "1",
"UniqueID": "2934041311738436184",
"Format": "VP8",
"CodecID": "V_VP8",
"Width": "1920",
"Height": "1080",
"PixelAspectRatio": "1.000",
"DisplayAspectRatio": "1.778",
"FrameRate_Mode": "CFR",
"FrameRate": "25.028",
"Compression_Mode": "Lossy",
"Delay": "0.000",
"Title": "Video",
"Language": "en",
"Default": "Yes",
"Forced": "No"
}
]
}
}

在 Ubuntu 中:

General
Count : 308
Count of stream of this kind : 1
Kind of stream : General
Kind of stream : General
Stream identifier : 0
Count of video streams : 1
Video_Format_List : VP8
Video_Format_WithHint_List : VP8
Codecs Video : V_VP8
Video_Language_List : English
Complete name : /var/kurento/tmp/27/23012020/1355kurento-recording.webm
Folder name : /var/kurento/tmp/27/23012020
File name : 1355kurento-recording
File extension : webm
Format : WebM
Format : WebM
Format/Url : http://www.webmproject.org/
Format/Extensions usually used : webm
Commercial name : WebM
Format version : Version 2
Internet media type : video/webm
Codec : WebM
Codec : WebM
Codec/Url : http://www.webmproject.org/
Codec/Extensions usually used : webm
File size : 1760787
File size : 1.68 MiB
File size : 2 MiB
File size : 1.7 MiB
File size : 1.68 MiB
File size : 1.679 MiB
Duration : 45037
Duration : 45s 37ms
Duration : 45s 37ms
Duration : 45s 37ms
Duration : 00:00:45.037
Duration : 00:00:45.037
Overall bit rate : 312772
Overall bit rate : 313 Kbps
Encoded date : UTC 2020-01-23 08:25:19
File last modification date : UTC 2020-01-23 08:26:04
File last modification date (local) : 2020-01-23 13:56:04
Writing application : GStreamer Matroska muxer
Writing application : GStreamer Matroska muxer
Writing library : GStreamer matroskamux version 1.8.1.1
Writing library : GStreamer matroskamux version 1.8.1.1

Video
Count : 311
Count of stream of this kind : 1
Kind of stream : Video
Kind of stream : Video
Stream identifier : 0
StreamOrder : 0
ID : 1
ID : 1
Unique ID : 3811409054155698551
Format : VP8
Format/Url : http://www.webmproject.org/
Commercial name : VP8
Codec ID : V_VP8
Codec ID/Url : http://www.webmproject.org/
Codec : V_VP8
Codec : V_VP8
Bit rate : 293486
Bit rate : 293 Kbps
Width : 1920
Width : 1 920 pixels
Height : 1080
Height : 1 080 pixels
Pixel aspect ratio : 1.000
Display aspect ratio : 1.778
Display aspect ratio : 16:9
Frame rate mode : VFR
Frame rate mode : Variable
Compression mode : Lossy
Compression mode : Lossy
Delay : 0
Delay : 00:00:00.000
Delay, origin : Container
Delay, origin : Container
Title : Video
Language : en
Language : English
Language : English
Language : en
Language : eng
Language : en
Default : Yes
Default : Yes
Forced : No
Forced : No

不知道结构差异的原因。仍在调试查找原因。任何帮助将不胜感激。

最佳答案

很高兴您自己发现了这个问题,正如您所看到的,Ubuntu 中的 stdout 不是有效的 JSON 字符串,并且 lib 中的代码执行 JSON.parse 解析为对象,因此它抛出错误。 enter image description here

我建议您不要使用该库,因为该库本身未经测试。如果您有其他选择,我建议您选择:https://github.com/caffco/get-video-duration

安装

$ npm install --save get-video-duration

使用

const { getVideoDurationInSeconds } = require('get-video-duration')

// From a local path...
getVideoDurationInSeconds('video.mov').then((duration) => {
console.log(duration)
})

// From a URL...
getVideoDurationInSeconds('http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4').then((duration) => {
console.log(duration)
})

// From a readable stream...

const fs = require('fs')
const stream = fs.createReadStream('video.mov')

getVideoDurationInSeconds(stream).then((duration) => {
console.log(duration)
})

关于node.js - JSON 中视频长度位置 0 处出现意外标记 G,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59894248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com