|
| 1 | +""" |
| 2 | +Path: examples/add_media_profile.py |
| 3 | +Author: @kaburagisec |
| 4 | +Created: October 30, 2025 |
| 5 | +Tested devices: EZVIZ H8C (https://www.ezviz.com/inter/product/h8c/43162) |
| 6 | +
|
| 7 | +This script connects to an ONVIF-compliant device and creates |
| 8 | +a new media profile with video encoder configuration using the Media service. |
| 9 | +
|
| 10 | +Steps: |
| 11 | +1. Connect to ONVIF device |
| 12 | +2. Get available video sources |
| 13 | +3. Get available video encoder configurations |
| 14 | +4. Create a new media profile |
| 15 | +5. Add video source configuration to the profile |
| 16 | +6. Add video encoder configuration to the profile |
| 17 | +7. Verify the profile was created successfully |
| 18 | +""" |
| 19 | + |
| 20 | +from onvif import ONVIFClient |
| 21 | + |
| 22 | +HOST = "192.168.1.3" |
| 23 | +PORT = 80 |
| 24 | +USERNAME = "admin" |
| 25 | +PASSWORD = "admin123" |
| 26 | + |
| 27 | +try: |
| 28 | + # Initialize ONVIF client |
| 29 | + client = ONVIFClient(HOST, PORT, USERNAME, PASSWORD) |
| 30 | + media = client.media() |
| 31 | + |
| 32 | + # Get available video sources |
| 33 | + print("Getting video sources...") |
| 34 | + video_sources = media.GetVideoSources() |
| 35 | + if not video_sources: |
| 36 | + print("No video sources available") |
| 37 | + exit(1) |
| 38 | + |
| 39 | + print(f"Found {len(video_sources)} video source(s)") |
| 40 | + video_source_token = video_sources[0].token |
| 41 | + print(f"Using video source token: {video_source_token}") |
| 42 | + |
| 43 | + # Get available video source configurations |
| 44 | + print("\nGetting video source configurations...") |
| 45 | + video_source_configs = media.GetVideoSourceConfigurations() |
| 46 | + if not video_source_configs: |
| 47 | + print("No video source configurations available") |
| 48 | + exit(1) |
| 49 | + |
| 50 | + video_source_config_token = video_source_configs[0].token |
| 51 | + print(f"Using video source configuration token: {video_source_config_token}") |
| 52 | + |
| 53 | + # Get available video encoder configurations |
| 54 | + print("\nGetting video encoder configurations...") |
| 55 | + video_encoder_configs = media.GetVideoEncoderConfigurations() |
| 56 | + if not video_encoder_configs: |
| 57 | + print("No video encoder configurations available") |
| 58 | + exit(1) |
| 59 | + |
| 60 | + video_encoder_config_token = video_encoder_configs[0].token |
| 61 | + print(f"Using video encoder configuration token: {video_encoder_config_token}") |
| 62 | + |
| 63 | + # Create a new media profile |
| 64 | + profile_name = "CustomProfile_001" |
| 65 | + print(f"\nCreating new media profile: {profile_name}") |
| 66 | + new_profile = media.CreateProfile(Name=profile_name) |
| 67 | + profile_token = new_profile.token |
| 68 | + print(f"Profile created with token: {profile_token}") |
| 69 | + |
| 70 | + # Add video source configuration to the profile |
| 71 | + print("\nAdding video source configuration to profile...") |
| 72 | + media.AddVideoSourceConfiguration( |
| 73 | + ProfileToken=profile_token, ConfigurationToken=video_source_config_token |
| 74 | + ) |
| 75 | + print("Video source configuration added successfully") |
| 76 | + |
| 77 | + # Add video encoder configuration to the profile |
| 78 | + print("\nAdding video encoder configuration to profile...") |
| 79 | + media.AddVideoEncoderConfiguration( |
| 80 | + ProfileToken=profile_token, ConfigurationToken=video_encoder_config_token |
| 81 | + ) |
| 82 | + print("Video encoder configuration added successfully") |
| 83 | + |
| 84 | + # Verify the profile was created |
| 85 | + print("\nVerifying profile creation...") |
| 86 | + profile = media.GetProfile(ProfileToken=profile_token) |
| 87 | + print(f"\nProfile Details:") |
| 88 | + print(f" Name: {profile.Name}") |
| 89 | + print(f" Token: {profile.token}") |
| 90 | + if hasattr(profile, "VideoSourceConfiguration") and profile.VideoSourceConfiguration: |
| 91 | + print( |
| 92 | + f" Video Source: {profile.VideoSourceConfiguration.SourceToken if hasattr(profile.VideoSourceConfiguration, 'SourceToken') else 'N/A'}" |
| 93 | + ) |
| 94 | + if ( |
| 95 | + hasattr(profile, "VideoEncoderConfiguration") |
| 96 | + and profile.VideoEncoderConfiguration |
| 97 | + ): |
| 98 | + print( |
| 99 | + f" Video Encoder: {profile.VideoEncoderConfiguration.Encoding if hasattr(profile.VideoEncoderConfiguration, 'Encoding') else 'N/A'}" |
| 100 | + ) |
| 101 | + |
| 102 | + print("\n✓ Media profile created and configured successfully!") |
| 103 | + |
| 104 | + # Optional: Get stream URI for the new profile |
| 105 | + print("\nGetting stream URI for the new profile...") |
| 106 | + stream_setup = {"Stream": "RTP-Unicast", "Transport": {"Protocol": "RTSP"}} |
| 107 | + stream_uri = media.GetStreamUri(StreamSetup=stream_setup, ProfileToken=profile_token) |
| 108 | + print(f"Stream URI: {stream_uri.Uri}") |
| 109 | + |
| 110 | +except Exception as e: |
| 111 | + print(f"Error: {e}") |
| 112 | + import traceback |
| 113 | + |
| 114 | + traceback.print_exc() |
0 commit comments