Неожиданный токен G в JSON в позиции 0, длина видео - PullRequest
0 голосов
/ 24 января 2020

Я использую видео-длина , чтобы получить продолжительность видео в 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. Пожалуйста, помогите мне, если я делаю что-то не так.

После отладки видео продолжительностью. js, я обнаружил ниже разницу в stdout, полученном в 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

Не знаю причину различия в структуре. Все еще отлаживаю по причине. Любая помощь будет высоко оценена.

1 Ответ

3 голосов
/ 27 января 2020

Хорошо, что вы нашли проблему самостоятельно, поскольку вы можете видеть, что stdout в Ubuntu не является допустимой строкой JSON, а код в lib делает JSON.parse для анализа объекта, следовательно, он выдает ошибку , enter image description here

Я бы посоветовал вам не использовать эту библиотеку, поскольку сама эта библиотека не тестировалась. Если у вас есть другой вариант, я бы предложил вам go с этим: 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)
})     
...