When running the following: ffmpeg -hwaccel cuda -hwacceloutputformat cuda -i…
- 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?
- Assistant
The error you're encountering occurs because the
subtitlesfilter operates in software and expects frames in system (CPU) memory, but your frames are currently in GPU memory due to hardware acceleration (-hwaccel cuda). Theauto_scale_0filter 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
subtitlesfilter and then, if necessary, upload them back to the GPU for encoding. You can achieve this by adjusting your filter chain to includehwdownload(which downloads frames from GPU to CPU memory) andhwupload_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.mp4Explanation:
hwdownload: Downloads the frames from GPU memory to CPU memory, making them accessible to software filters likesubtitles.format=nv12: Converts the pixel format tonv12, which is compatible with both thesubtitlesfilter 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 withh264_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 cudaspecifies that the decoded frames should remain in GPU memory, which is why we needhwdownloadto bring them back to the CPU for processing withsubtitles.
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.