You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

技术求助:如何禁止视频应用导入竖屏拍摄的视频?

How to Block Portrait Video Capture & Imports in Your Video App

Hey there! Let's tackle your video app requirements with concrete, actionable steps. Here's how you can block portrait video capture in-app and prevent portrait video imports effectively:

1. Disable In-App Portrait Video Capture

  • Lock Camera Orientation & Resolution
    If you're building a native iOS/Android app, you can enforce landscape-only capture directly via the system camera APIs:
    • For iOS: Configure your AVCaptureSession to use a landscape preset (like AVCaptureSessionPreset1920x1080), and lock your camera view controller's orientation to UIInterfaceOrientationMaskLandscape. This ensures the camera only captures in a横向 aspect ratio.
    • For Android: In your camera activity, set the screen orientation to LANDSCAPE in the manifest or programmatically. Then use Camera.Parameters to set preview and picture sizes to landscape dimensions (e.g., 1920x1080 instead of 1080x1920).
  • Custom Camera Logic Checks
    If you're using a custom camera component, add a check for device orientation. If the user tries to capture while holding the device vertically, show a clear prompt like "Please rotate your device to landscape mode to record" and disable the capture button until the orientation changes.

2. Prevent Portrait Video Imports

The core here is detecting the video's actual display aspect ratio before allowing import. Here's how to implement it across platforms:

Detect Video Aspect Ratio (with Rotation Handling)

Many videos have rotation metadata (e.g., a video shot vertically but tagged to play horizontally). You need to account for this to avoid false positives.

Android Example

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(videoFilePath);

// Get raw width/height and rotation
int width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
int rotation = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));

retriever.release();

// Adjust for rotation (90/270 degrees swap width/height)
if (rotation == 90 || rotation == 270) {
    int temp = width;
    width = height;
    height = temp;
}

// Check if it's portrait (height > width)
if (height > width) {
    // Block import: Show user prompt
    Toast.makeText(context, "Sorry, portrait videos are not supported for import.", Toast.LENGTH_LONG).show();
    return;
}

iOS Example

guard let videoURL = URL(string: importedVideoPath) else { return }
let asset = AVAsset(url: videoURL)

guard let videoTrack = asset.tracks(withMediaType: .video).first else {
    // Handle invalid video
    return
}

// Get transformed size (accounts for rotation metadata)
let transformedSize = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
let isPortrait = transformedSize.height > transformedSize.width

if isPortrait {
    // Block import: Show alert to user
    let alert = UIAlertController(title: "Unsupported Video", message: "Portrait videos can't be imported. Please select a landscape video.", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default))
    present(alert, animated: true)
    return
}

Edge Case Handling

  • Square Videos: If you want to block square videos too, adjust the check to reject videos where width <= height * 1.2 (tweak the multiplier based on your allowed aspect ratios like 16:9 or 4:3).
  • Performance: Using native media metadata APIs (like MediaMetadataRetriever or AVAsset) is lightweight—you don't need to decode the entire video, just read the header metadata.

Final Tips

Always give clear, user-friendly feedback instead of silently blocking actions. Users need to understand why their video is being rejected to avoid frustration.

内容的提问来源于stack exchange,提问作者user3634131

火山引擎 最新活动