This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextMediaResizer.cs
157 lines (138 loc) · 5.65 KB
/
TextMediaResizer.cs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Sony.Vegas;
public class EntryPoint
{
/// <summary>
/// テキストメディアを文字数に応じてメディアサイズを変える
/// </summary>
private const double TimePerWordCountms = 115;
private const double TimePerCommams = 200;
private const double ExtraTimems = 500;
private const string TitleNamePrefix = "Sony タイトルおよびテキスト ";
//VegasだとvarとかC#3.0のLinqのメソッド形式の入力がエラー
public void FromVegas(Vegas vegas)
{
// 選択されたテキストイベントを取得(Take名が指定の名称プレフィックスと一致している場合のみ)
List<TrackEvent> selectedTextEvents = new List<TrackEvent>();
foreach (TrackEvent ev in FindSelectedEvents(vegas.Project))
{
if (ev.ActiveTake.Name.IndexOf(TitleNamePrefix, StringComparison.Ordinal) != -1)
selectedTextEvents.Add(ev);
}
// 条件を満たしているかどうかのチェック
if (!CheckConfiguration(selectedTextEvents))
return;
Dictionary<TrackEvent,double> selectedTextEventsTimesms = new Dictionary<TrackEvent, double>();
foreach (TrackEvent ev in selectedTextEvents)
{
selectedTextEventsTimesms.Add(ev, CalculateEventLengthBasedOnWordCount(ev));
}
// ダイアログのメッセージ生成
string message = MakeDialogMessage(selectedTextEventsTimesms);
// 確認ダイアログ生成
DialogResult result = MessageBox.Show(message,
"確認",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2);
if (result != DialogResult.OK)
return;
// 処理実行
foreach (TrackEvent textEvent in selectedTextEvents)
{
textEvent.Length = new Timecode(selectedTextEventsTimesms[textEvent]);
}
}
/// <summary>
/// スクリプトを実行する条件を満たしているかを確認する
/// </summary>
/// <param name="selectedTextEvents">選択されたテキストイベント</param>
/// <returns></returns>
bool CheckConfiguration(List<TrackEvent> selectedTextEvents)
{
if (selectedTextEvents.Count == 0)
{
MessageBox.Show("テキストイベントが選択されていません。");
}
else
{
return true;
}
return false;
}
string MakeDialogMessage(Dictionary<TrackEvent, double> evTimes)
{
string ans = String.Empty;
foreach (KeyValuePair<TrackEvent,double> evTime in evTimes)
{
ans += "テキスト : " + evTime.Key.ActiveTake.Name.Replace(TitleNamePrefix,"") + " 長さ : " + evTime.Value / 1000 + " 秒\n";
}
ans += "に変更します。";
return ans;
}
TrackEvent[] FindSelectedEvents(Project project)
{
List<TrackEvent> selectedEvents = new List<TrackEvent>();
foreach (Track track in project.Tracks)
{
foreach (TrackEvent trackEvent in track.Events)
{
if (trackEvent.Selected)
{
selectedEvents.Add(trackEvent);
}
}
}
return selectedEvents.ToArray();
}
/// <summary>
/// テキストイベントの文字数に応じて、イベントの長さを求めるメソッド
/// https://www.pine4.net/Memo/Article/Archives/424
/// </summary>
/// <param name="textEvent">変更するTrackEvent</param>
/// <returns>length of event</returns>
double CalculateEventLengthBasedOnWordCount(TrackEvent textEvent)
{
string takeName = textEvent.ActiveTake.Name.Remove(0, TitleNamePrefix.Length);
IFELanguage ifelang = null;
string allKanaName=String.Empty;
try
{
ifelang = Activator.CreateInstance(Type.GetTypeFromProgID("MSIME.Japan")) as IFELanguage;
int hr = ifelang.Open();
if (hr != 0)
throw Marshal.GetExceptionForHR(hr);
hr = ifelang.GetPhonetic(takeName, 1, -1, out allKanaName);
ifelang.Close();
}
catch (COMException ex)
{
if (ifelang != null)
ifelang.Close();
}
int commaNumber = CountChar(allKanaName, ',') + CountChar(allKanaName, '、');
int spaceNumber = CountChar(allKanaName, ' ') + CountChar(allKanaName, ' ');
return (allKanaName.Length - commaNumber - spaceNumber) * TimePerWordCountms + commaNumber * TimePerCommams + ExtraTimems;
}
int CountChar(string s, char c)
{
return s.Length - s.Replace(c.ToString(), "").Length;
}
// IFELanguage2 Interface ID
//[Guid("21164102-C24A-11d1-851A-00C04FCC6B14")]
[ComImport]
[Guid("019F7152-E6DB-11d0-83C3-00C04FDDB82E")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IFELanguage
{
int Open();
int Close();
int GetJMorphResult(uint dwRequest, uint dwCMode, int cwchInput, [MarshalAs(UnmanagedType.LPWStr)] string pwchInput, IntPtr pfCInfo, out object ppResult);
int GetConversionModeCaps(ref uint pdwCaps);
int GetPhonetic([MarshalAs(UnmanagedType.BStr)] string @string, int start, int length, [MarshalAs(UnmanagedType.BStr)] out string result);
int GetConversion([MarshalAs(UnmanagedType.BStr)] string @string, int start, int length, [MarshalAs(UnmanagedType.BStr)] out string result);
}
}