forked from nscript-site/NScript.AndroidBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaSlicer.cs
More file actions
115 lines (93 loc) · 3.24 KB
/
MediaSlicer.cs
File metadata and controls
115 lines (93 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
namespace NScript.AndroidBot
{
using Geb.Image;
using Geb.Media.IO;
public class MediaSlicer
{
private Object SyncRoot = new object();
private MediaWriter MediaWriter;
public double FrameRate { get; set; }
private String _baseDir = "./slicer";
private DateTime Start = DateTime.Now;
/// <summary>
/// 切片的最大长度,单位是秒
/// </summary>
public double MaxDuration { get; set; } = 600;
public bool Enable { get; set; }
private List<Tuple<double, String>> History = new ();
public Tuple<double, String>[] GetHistory()
{
lock(SyncRoot)
{
return History.ToArray();
}
}
public String BaseDir
{
get { return _baseDir; }
set
{
if(_baseDir != value)
{
DirectoryInfo dirInfo = new DirectoryInfo(value);
if (dirInfo.Exists == false) dirInfo.Create();
lock(SyncRoot)
{
_baseDir = value;
if (MediaWriter != null)
{
MediaWriter.Close();
MediaWriter = null;
}
}
}
}
}
private String PrevFileName = String.Empty;
public void Receive(ImageBgr24 image)
{
if (Enable == false) return;
lock(SyncRoot)
{
DateTime now = DateTime.Now;
if(MediaWriter == null || (now - Start).TotalSeconds > MaxDuration)
{
if (MediaWriter != null)
{
History.Add(new Tuple<double, string>(MaxDuration, PrevFileName));
while (History.Count > 5)
History.RemoveAt(0);
MediaWriter.Close();
}
DirectoryInfo dirInfo = new DirectoryInfo(_baseDir);
if (dirInfo.Exists == false) dirInfo.Create();
FileInfo file = new FileInfo(Path.Combine(dirInfo.FullName, now.ToFileTimeUtc() + ".mp4"));
PrevFileName = file.Name;
MediaWriter = new MediaWriter(file.FullName, image.Width, image.Height, FrameRate);
Start = DateTime.Now;
lastAudioOffset = 0;
}
MediaWriter.WriteFrame(image);
}
}
private double lastAudioOffset = 0;
public unsafe void Receive(Byte[] audioData)
{
if (Enable == false) return;
double duration = (audioData.Length * 1000) / (2.8*4*44100);
lock (SyncRoot)
{
if (MediaWriter != null)
{
MediaWriter.WriteAudio(audioData, audioData.Length, 0);
lastAudioOffset += duration;
}
}
}
}
}