Skip to content

Commit d3ec7f5

Browse files
author
chenxinfeng
committed
speed up get stream infomation. change the get_info from xml string to json string
1 parent 520d4ea commit d3ec7f5

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ How can `toCUDA` make it faster in your deeplearning pipeline than `opencv` or `
237237
238238
## Video Writer
239239
---
240+
240241
```python
241242
# cv2
242243
out = cv2.VideoWriter('outpy.avi',
@@ -248,7 +249,8 @@ out.write(frame2)
248249
out.release()
249250

250251
# ffmpegcv, default codec is 'h264' in cpu 'h265' in gpu.
251-
# frameSize is decided by the size of the first frame
252+
# frameSize is decided by the size of the first frame.
253+
# use the 'mp4/mkv' instead of 'avi' to avoid the codec outdated.
252254
out = ffmpegcv.VideoWriter('outpy.mp4', None, 10)
253255
out.write(frame1)
254256
out.write(frame2)

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ out.release()
239239

240240
# ffmpegcv,默认的编码器为'h264'在CPU上,'h265'在GPU上。
241241
# 帧大小由第一帧决定
242+
# 使用 'mp4/mkv' 来替代古老的 'avi' 格式
242243
out = ffmpegcv.VideoWriter('outpy.mp4', None, 10)
243244
out.write(frame1)
244245
out.write(frame2)

ffmpegcv/stream_info.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import subprocess
22
from collections import namedtuple
3-
import xml.etree.ElementTree as ET
3+
import json
44
import shlex
55

66

7-
def get_info(stream_url, timeout=None):
8-
rtspflag = "-rtsp_transport tcp" if stream_url.startswith("rtsp://") else ""
9-
cmd = 'ffprobe -v quiet -print_format xml {} -select_streams v:0 -show_format -show_streams "{}"'.format(
10-
rtspflag, stream_url
11-
)
7+
def get_info(stream_url, timeout=None, duration_ms: int = 100):
8+
rtsp_opt = '' if not stream_url.startswith('rtsp://') else '-rtsp_flags prefer_tcp -pkt_size 736 '
9+
analyze_duration = f'-analyzeduration {duration_ms * 1000}'
10+
cmd = (f'ffprobe -v quiet -print_format json=compact=1 {rtsp_opt} {analyze_duration} '
11+
f'-select_streams v:0 -show_format -show_streams "{stream_url}"')
1212
output = subprocess.check_output(shlex.split(cmd), shell=False, timeout=timeout)
13-
root = ET.fromstring(output)
14-
assert (root[0].tag, root[0][0].tag) == ("streams", "stream")
15-
vinfo = root[0][0].attrib
13+
data: dict = json.loads(output)
14+
vinfo: dict = data['streams'][0]
1615

1716
StreamInfo = namedtuple(
1817
"StreamInfo", ["width", "height", "fps", "count", "codec", "duration"]

ffmpegcv/video_info.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from subprocess import Popen, PIPE
33
import re
44
from collections import namedtuple
5-
import xml.etree.ElementTree as ET
5+
import json
66
import shlex
77
import platform
88

@@ -19,14 +19,13 @@ def get_info(video: str):
1919
do_scan_the_whole = video.split(".")[-1] in scan_the_whole
2020

2121
def ffprobe_info_(do_scan_the_whole):
22-
use_count_packets = "-count_packets" if do_scan_the_whole else ""
23-
cmd = 'ffprobe -v quiet -print_format xml -select_streams v:0 {} -show_format -show_streams "{}"'.format(
24-
use_count_packets, video
25-
)
22+
use_count_packets = '-count_packets' if do_scan_the_whole else ''
23+
cmd = 'ffprobe -v quiet -print_format json=compact=1 -select_streams v:0 {} -show_streams "{}"'.format(
24+
use_count_packets, video)
25+
2626
output = subprocess.check_output(shlex.split(cmd), shell=False)
27-
root = ET.fromstring(output)
28-
assert (root[0].tag, root[0][0].tag) == ("streams", "stream")
29-
vinfo = root[0][0].attrib
27+
data: dict = json.loads(output)
28+
vinfo: dict = data['streams'][0]
3029
return vinfo
3130

3231
vinfo = ffprobe_info_(do_scan_the_whole)

0 commit comments

Comments
 (0)