Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

When running the following: ffmpeg -hwaccel cuda -hwacceloutputformat cuda -i…

  1. User

    When running the following:

    ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i yt_download.mp4 -vf format=yuv420p,subtitles=yt_download.en.srt -c:v h264_nvenc -profile:v high -level 4.0 -preset fast -b:v 0 -cq 35 -c:a copy out-gpu.mp4

    I get this error:

    Impossible to convert between the formats supported by the filter 'graph 0 input from stream 0:0' and the filter 'auto_scale_0'

    How do I fix it?

  2. Assistant

    The error you're encountering occurs because the subtitles filter operates in software and expects frames in system (CPU) memory, but your frames are currently in GPU memory due to hardware acceleration (-hwaccel cuda). The auto_scale_0 filter is implicitly inserted by FFmpeg to handle format conversions, but it can't bridge the gap between GPU and CPU memory without explicit instructions.

    To fix this issue, you need to download the frames from the GPU back to system memory before applying the subtitles filter and then, if necessary, upload them back to the GPU for encoding. You can achieve this by adjusting your filter chain to include hwdownload (which downloads frames from GPU to CPU memory) and hwupload_cuda (which uploads frames from CPU back to GPU memory).

    Here's how you can modify your command:

    ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i yt_download.mp4 \
    -vf "hwdownload,format=nv12,subtitles=yt_download.en.srt,hwupload_cuda" \
    -c:v h264_nvenc -profile:v high -level 4.0 -preset fast -b:v 0 -cq 35 -c:a copy out-gpu.mp4
    

    Explanation:

    • hwdownload: Downloads the frames from GPU memory to CPU memory, making them accessible to software filters like subtitles.
    • format=nv12: Converts the pixel format to nv12, which is compatible with both the subtitles filter and NVIDIA encoding.
    • subtitles=yt_download.en.srt: Applies your subtitle overlay to the frames in CPU memory.
    • hwupload_cuda: Uploads the frames back to GPU memory for hardware encoding with h264_nvenc.

    Notes:

    • Ensure that your FFmpeg build includes support for CUDA and h264_nvenc.
    • If you encounter any color or format issues, you might need to adjust the pixel formats (format= filters) to match your input and desired output.
    • The use of -hwaccel_output_format cuda specifies that the decoded frames should remain in GPU memory, which is why we need hwdownload to bring them back to the CPU for processing with subtitles.

    By modifying your command as shown, you should be able to overlay subtitles onto your video while leveraging GPU acceleration for decoding and encoding, thereby resolving the format conversion error.