11
11
using System . Linq ;
12
12
using System . Text ;
13
13
using System . Threading . Tasks ;
14
+ using static ClassTranscribeDatabase . CommonUtils ;
14
15
15
16
namespace ClassTranscribeServer . Controllers
16
17
{
@@ -47,23 +48,70 @@ public async Task<ActionResult> UpdateSceneData(string videoId, JObject scene)
47
48
{
48
49
string sceneAsString = scene . ToString ( 0 ) ;
49
50
Video video = await _context . Videos . FindAsync ( videoId ) ;
50
- if ( video . HasSceneObjectData ( ) )
51
+ var existingScenes = video . HasSceneObjectData ( ) ;
52
+
53
+ TextData data ;
54
+ if ( existingScenes )
51
55
{
52
- TextData data = await _context . TextData . FindAsync ( video . SceneObjectDataId ) ;
56
+ data = await _context . TextData . FindAsync ( video . SceneObjectDataId ) ;
53
57
data . Text = sceneAsString ;
54
58
} else
55
59
{
56
- TextData data = new TextData ( ) ;
57
- data . Text = sceneAsString ;
60
+ data = new TextData ( ) { Text = sceneAsString } ;
58
61
_context . TextData . Add ( data ) ;
59
62
video . SceneObjectDataId = data . Id ;
60
63
Trace . Assert ( ! string . IsNullOrEmpty ( data . Id ) ) ;
61
64
}
62
-
65
+
66
+ createDescriptionsIfNone ( video , data ) ;
63
67
await _context . SaveChangesAsync ( ) ;
64
68
return Ok ( ) ;
65
69
}
66
- [ HttpGet ( "GetPhraseHints" ) ]
70
+ private void createDescriptionsIfNone ( Video v , TextData scenedata )
71
+ {
72
+ JArray scenes = scenedata . getAsJSON ( ) [ "Scenes" ] as JArray ;
73
+ if ( scenes == null || v == null || v . Id == null )
74
+ {
75
+ return ;
76
+ }
77
+
78
+ var exists = v . Transcriptions . Exists ( t=> t . TranscriptionType == TranscriptionType . TextDescription ) ;
79
+ if ( exists )
80
+ {
81
+ _logger . LogInformation ( $ "{ v . Id } : already has descriptions (skipping)") ;
82
+ return ;
83
+ }
84
+ _logger . LogInformation ( $ "{ v . Id } : Creating basic descriptions") ;
85
+ var captions = new List < Caption > ( ) ;
86
+
87
+ int index = 0 ;
88
+ foreach ( JObject scene in scenes )
89
+ {
90
+ var c = new Caption
91
+ {
92
+ Index = index ++ ,
93
+ Begin = TimeSpan . Parse ( scene [ "start" ] . ToString ( ) ) ,
94
+ End = TimeSpan . Parse ( scene [ "end" ] . ToString ( ) ) ,
95
+ CaptionType = CaptionType . AudioDescription ,
96
+ Text = scene [ "phrases" ] ? . ToString ( )
97
+ } ;
98
+ }
99
+ _logger . LogInformation ( $ "{ v . Id } : { index } entries added") ;
100
+ var transcription = new Transcription ( )
101
+ {
102
+ Captions = captions ,
103
+ TranscriptionType = TranscriptionType . TextDescription ,
104
+ VideoId = v . Id ,
105
+ Language = Languages . ENGLISH_AMERICAN ,
106
+ Label = "Description" ,
107
+ SourceLabel = "ClassTranscribe" ,
108
+ SourceInternalRef = "ClassTranscribe/Scene-OCR"
109
+ } ;
110
+
111
+ _context . Add ( transcription ) ;
112
+ }
113
+
114
+ [ HttpGet ( "GetPhraseHints" ) ]
67
115
public async Task < string > GetPhraseHints ( string videoId ) {
68
116
Video video = await _context . Videos . FindAsync ( videoId ) ;
69
117
if ( video . HasPhraseHints ( ) ) {
@@ -74,6 +122,8 @@ public async Task<string> GetPhraseHints(string videoId) {
74
122
return video . PhraseHints ?? "" ;
75
123
}
76
124
125
+
126
+
77
127
[ HttpGet ( "GetSceneData" ) ]
78
128
public async Task < ActionResult < Object > > GetSceneData ( string videoId ) {
79
129
Video video = await _context . Videos . FindAsync ( videoId ) ;
0 commit comments