--- title: "실시간 스트리밍 인식" slug: "ai-application-service-clovaspeech-example02" updated: 2026-07-23T09:04:22Z published: 2026-07-23T09:04:22Z canonical: "api.ncloud-docs.com/ai-application-service-clovaspeech-example02" --- > ## Documentation Index > Fetch the complete documentation index at: https://api.ncloud-docs.com/llms.txt > Use this file to discover all available pages before exploring further. # 실시간 스트리밍 인식 Classic/VPC 환경에서 이용 가능합니다. CLOVA Speech 서비스의 실시간 스트리밍 예제를 소개합니다. ## Java Java 기반의 API 예제 코드는 다음과 같습니다. - Project Structure ``` ├───pom.xml │ │ └───src │ ├───main │ │ ├───java │ │ │ └───com │ │ │ └───example │ │ │ └───grpc │ │ │ GRpcClient.java │ │ │ │ │ ├───proto │ │ │ nest.proto ``` - pom.xml ``` 4.0.0 com.example clova-speech-grpc 1.0-SNAPSHOT 1.8 ${java.version} ${java.version} UTF-8 UTF-8 4.1.52.Final 1.35.0 3.14.0 io.grpc grpc-netty ${grpc.version} io.grpc grpc-netty-shaded ${grpc.version} io.grpc grpc-protobuf ${grpc.version} io.grpc grpc-stub ${grpc.version} org.projectlombok lombok true 1.18.12 kr.motd.maven os-maven-plugin 1.6.1 org.apache.maven.plugins maven-compiler-plugin 3.1 compile compile compile testCompile test-compile testCompile true ${project.build.sourceEncoding} org.xolstice.maven.plugins protobuf-maven-plugin 0.6.1 com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} grpc-java io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} compile compile-custom ``` - Java ``` package com.example.grpc; import java.io.FileInputStream; import java.util.concurrent.CountDownLatch; import com.google.protobuf.ByteString; import com.nbp.cdncp.nest.grpc.proto.v1.NestConfig; import com.nbp.cdncp.nest.grpc.proto.v1.NestData; import com.nbp.cdncp.nest.grpc.proto.v1.NestRequest; import com.nbp.cdncp.nest.grpc.proto.v1.NestResponse; import com.nbp.cdncp.nest.grpc.proto.v1.NestServiceGrpc; import com.nbp.cdncp.nest.grpc.proto.v1.RequestType; import io.grpc.ManagedChannel; import io.grpc.Metadata; import io.grpc.StatusRuntimeException; import io.grpc.netty.NettyChannelBuilder; import io.grpc.stub.MetadataUtils; import io.grpc.stub.StreamObserver; public class GRpcClient { public static void main(String[] args) throws Exception { CountDownLatch latch = new CountDownLatch(1); ManagedChannel channel = NettyChannelBuilder .forTarget("clovaspeech-gw.ncloud.com:50051") .useTransportSecurity() .build(); NestServiceGrpc.NestServiceStub client = NestServiceGrpc.newStub(channel); Metadata metadata = new Metadata(); metadata.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), "Bearer ${secretKey}"); client = MetadataUtils.attachHeaders(client, metadata); StreamObserver responseObserver = new StreamObserver() { @Override public void onNext(NestResponse response) { System.out.println("Received response: " + response.getContents()); } @Override public void onError(Throwable t) { if(t instanceof StatusRuntimeException) { StatusRuntimeException error = (StatusRuntimeException)t; System.out.println(error.getStatus().getDescription()); } latch.countDown(); } @Override public void onCompleted() { System.out.println("completed"); latch.countDown(); } }; StreamObserver requestObserver = client.recognize(responseObserver); requestObserver.onNext(NestRequest.newBuilder() .setType(RequestType.CONFIG) .setConfig(NestConfig.newBuilder() .setConfig("{\"transcription\":{\"language\":\"ko\"}}") .build()) .build()); java.io.File file = new java.io.File("~/media/42s.wav"); byte[] buffer = new byte[32000]; int bytesRead; FileInputStream inputStream = new FileInputStream(file); while ((bytesRead = inputStream.read(buffer)) != -1) { requestObserver.onNext(NestRequest.newBuilder() .setType(RequestType.DATA) .setData(NestData.newBuilder() .setChunk(ByteString.copyFrom(buffer, 0, bytesRead)) .setExtraContents("{ \"seqId\": 0, \"epFlag\": false}") .build()) .build()); } requestObserver.onCompleted(); latch.await(); channel.shutdown(); } } ``` ## Python Python 기반의 API 예제 코드는 다음과 같습니다. ``` import grpc import json import nest_pb2 import nest_pb2_grpc AUDIO_PATH = "path/to/audio/file" #인식할 오디오 파일이 위치한 경로를 입력해 주십시오. (16kHz, 1channel, 16 bits per sample의 PCM (헤더가 없는 raw wave) 형식) CLIENT_SECRET = "장문인식 secretKey" def generate_requests(audio_path): # 초기 설정 요청: 음성 인식 설정 yield nest_pb2.NestRequest( type=nest_pb2.RequestType.CONFIG, config=nest_pb2.NestConfig( config=json.dumps({"transcription": {"language": "ko"}}) ) ) # 오디오 파일을 열고 32,000 바이트씩 읽음 with open(audio_path, "rb") as audio_file: while True: chunk = audio_file.read(32000) # 오디오 파일의 청크를 읽음 if not chunk: break # 데이터가 더 이상 없으면 루프 종료 yield nest_pb2.NestRequest( type=nest_pb2.RequestType.DATA, data=nest_pb2.NestData( chunk=chunk, extra_contents=json.dumps({"seqId": 0, "epFlag": False}) ) ) def main(): # Clova Speech 서버에 대한 보안 gRPC 채널을 설정 channel = grpc.secure_channel( "clovaspeech-gw.ncloud.com:50051", grpc.ssl_channel_credentials() ) stub = nest_pb2_grpc.NestServiceStub(channel) # NestService에 대한 stub 생성 metadata = (("authorization", f"Bearer {CLIENT_SECRET}"),) # 인증 토큰과 함께 메타데이터 설정 responses = stub.recognize(generate_requests(AUDIO_PATH), metadata=metadata) # 생성된 요청으로 인식(recognize) 메서드 호출 try: # 서버로부터 응답을 반복 처리 for response in responses: print("Received response: " + response.contents) except grpc.RpcError as e: # gRPC 오류 처리 print(f"Error: {e.details()}") finally: channel.close() # 작업이 끝나면 채널 닫기 if __name__ == "__main__": main() ```