M: I need to read the exact codec settings from an mov file using ffmpeg, then use those settings to turn a png file into a 5 second mov file using those settings, for the purpose of concatenation. Can you help with this.
G: To achieve this, you'll follow these steps using FFmpeg:
Extract the codec settings from the source
.mov file: Use FFmpeg's ffprobe tool
to read the codec settings of the .mov file.
Create the .mov file from the
.png using the extracted settings: Use the
extracted settings to encode your PNG into a .mov
file.
Ensure compatibility for concatenation: Ensure
the output file is compatible with the original .mov for
seamless concatenation.
Run the following command to get codec information:
ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,codec_tag_string,width,height,r_frame_rate,pix_fmt -of default=nw=1:nk=1 input.movThis will give you:
codec_name: The codec (e.g., prores,
h264, etc.).codec_tag_string: Codec tag string.width and height: Resolution.r_frame_rate: Frame rate.pix_fmt: Pixel format..mov from the .pngUse the extracted settings to encode the .png file into
a 5-second .mov. For example, suppose the settings are:
prores1920x108030 fpsyuv422p10leRun the following command:
ffmpeg -loop 1 -i input.png -c:v prores -pix_fmt yuv422p10le -s 1920x1080 -r 30 -t 5 output.movExplanation:
-loop 1: Loops the PNG to create a video.-c:v prores: Sets the codec to
prores.-pix_fmt yuv422p10le: Matches the pixel format.-s 1920x1080: Sets the resolution.-r 30: Sets the frame rate.-t 5: Specifies a 5-second duration.Concatenate the files using FFmpeg's concat demuxer. First, create a
text file file_list.txt with the following content:
file 'input.mov'
file 'output.mov'
Then run:
ffmpeg -f concat -safe 0 -i file_list.txt -c copy concatenated.movThis will combine input.mov and output.mov
without re-encoding, preserving quality.
If you encounter specific issues (e.g., codec mismatch), let me know, and we can adjust the process further!
M: What if probe reports pix_fmt as (base)?
G: If ffprobe reports
pix_fmt as (base), it typically means the
pixel format is not explicitly reported or may be interpreted
differently by FFmpeg. In such cases, FFmpeg defaults to the "base"
pixel format for the codec used (e.g., yuv420p for
H.264).
To handle this:
Identify the Default Pixel Format for the Codec:
prores, h264)
reported by ffprobe.yuv420p.yuv422p10le or
yuv444p10le.Test with Common Pixel Formats: If you're
unsure, you can experiment with common formats like
yuv420p, yuv422p, or yuv444p.
Most codecs will reject incompatible pixel formats, so FFmpeg will
notify you if you choose the wrong one.
Set Pixel Format Explicitly: In your FFmpeg
encoding command, include the -pix_fmt option with a
standard format (e.g., yuv420p):
ffmpeg -loop 1 -i input.png -c:v h264 -pix_fmt yuv420p -s 1920x1080 -r 30 -t 5 output.movLeave -pix_fmt Out if Compatible:
If your initial .mov is straightforward (e.g., H.264 with
yuv420p), omitting the -pix_fmt flag might
work because FFmpeg will use the default pixel format for the codec:
ffmpeg -loop 1 -i input.png -c:v h264 -s 1920x1080 -r 30 -t 5 output.movInspect Original File for Exact Pixel Format: Run the following to attempt more detailed inspection of the original file:
ffprobe -v error -show_streams input.movLook for pix_fmt under the video stream. If still
unclear, try ffmpeg -i input.mov for additional
hints.
If you're still unsure or encounter errors, share the codec name and
other details extracted from ffprobe, and I can refine the
steps further!
M: Please repeat those instructions, incorporating the fact that the original mov has an audio stream
G: If the original
.mov file includes an audio stream, you need to handle both
the video and audio streams to ensure compatibility for concatenation.
Here’s how to adjust the process:
Run ffprobe to gather codec details for both video and
audio:
ffprobe -v error -show_streams -select_streams v:0 input.movThis extracts video settings (e.g., codec_name,
width, height, r_frame_rate,
pix_fmt). Then, run for audio:
ffprobe -v error -show_streams -select_streams a:0 input.movLook for:
h264, prores).yuv420p for H.264).width and height).r_frame_rate).aac).44100 Hz or 48000
Hz).stereo).You can include the original audio stream in your new file or add silence. Here's how:
If you don't have an audio source, generate silence matching the original audio properties:
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-loop 1 -i input.png -c:v h264 -pix_fmt yuv420p -s 1920x1080 -r 30 \
-t 5 -shortest -c:a aac -ar 44100 -b:a 128k output.movExtract the audio from the original .mov file and add it
to your new .mov:
Extract Audio:
ffmpeg -i input.mov -vn -c:a copy audio.m4aCreate the Video with Audio:
ffmpeg -loop 1 -i input.png -i audio.m4a -c:v h264 -pix_fmt yuv420p \
-s 1920x1080 -r 30 -t 5 -shortest -c:a aac output.movPrepare a file_list.txt with the following content:
file 'input.mov'
file 'output.mov'
Then concatenate the files:
ffmpeg -f concat -safe 0 -i file_list.txt -c copy concatenated.movpix_fmt is unclear,
default to yuv420p for H.264 or yuv422p for
ProRes. Test compatibility.output.mov are exactly 5 seconds.If you run into issues (e.g., mismatched codecs, incompatible audio), share details, and I can help troubleshoot further!
M: The concatenation keeps throwing “[vost#0:0/copy @ 0x55b137943600] non-monotonic dts “ warnings at me, and the merged file only shows the first file.
G: The
non-monotonic DTS error and the issue of the concatenated
file only showing the first input occur when the timestamps of the video
streams in the concatenated files are not continuous or compatible. To
resolve this issue, ensure that all input files have properly aligned
and compatible streams for concatenation.
Here’s how to fix it:
When files aren't natively compatible, you must re-encode them to ensure consistent codec, resolution, frame rate, pixel format, and audio properties. Follow these steps:
Prepare Each File for Concatenation: Re-encode
both the original .mov and the new .mov
created from the PNG to ensure compatibility. Use the following command
for each file:
ffmpeg -i input.mov -c:v libx264 -pix_fmt yuv420p -r 30 -c:a aac -ar 44100 -ac 2 reencoded_input.movFor the PNG-based file:
ffmpeg -loop 1 -i input.png -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-c:v libx264 -pix_fmt yuv420p -r 30 -t 5 -c:a aac -shortest reencoded_output.movCreate a File List for Concatenation: Create a
text file file_list.txt with the following content:
file 'reencoded_input.mov'
file 'reencoded_output.mov'Concatenate the Files: Run the concatenation command:
ffmpeg -f concat -safe 0 -i file_list.txt -c copy concatenated.movIf you need to debug timestamp issues in the original files, you can inspect the DTS/PTS values using:
ffprobe -show_packets -select_streams v:0 input.movThis will let you verify whether there are inconsistencies in the timestamps of the original files.
If re-encoding isn't desired and the input files are supposed to be compatible (same codec, resolution, frame rate, pixel format, and audio), you can use the concat protocol. However, this requires binary compatibility:
Convert Inputs to a Compatible Raw Stream: Run:
ffmpeg -i input.mov -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i output.mov -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.tsConcatenate the Streams: Then, concatenate them:
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc concatenated.mov-t explicitly to set
the duration.ffprobe to ensure properties align.Let me know if any step isn’t clear or if further troubleshooting is needed!
M: Okay, in comparing the ffprobe outputs, the audio streams are the same, while the video streams are thus: Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1920x1080, 10549 kb/s, 60 fps, 60 tbr, 60k tbn (default) Video: h264 (High) (avc1 /0x31637661), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 372 kb/s, 60 fps, 60 tbr, 15360 tbn (default) Can you tell me where the incompatibility is occurring based on that?
G: The incompatibility likely
arises from differences in the bitrate and the
timebase (tbn) between the two video
streams:
Bitrate:
10549 kb/s372 kb/sTimebase (tbn):
60k tbn (60,000 ticks per second)15360 tbn (15,360 ticks per second)tbn to calculate timestamps, and mismatched timebases cause
DTS/PTS inconsistencies (non-monotonic dts warnings).You need to standardize the tbn, bitrate,
and other settings between the two files. Follow these steps:
Re-encode the PNG-based .mov file to align with the
properties of the first .mov file (the higher-quality
source). Use the following FFmpeg command:
ffmpeg -loop 1 -i input.png -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-c:v h264 -profile:v high -pix_fmt yuv420p -s 1920x1080 -b:v 10549k -r 60 -t 5 \
-timebase 1/60000 -c:a aac -ar 44100 -ac 2 reencoded_output.movExplanation:
-b:v 10549k: Matches the video bitrate of the first
file.-r 60: Matches the frame rate of the first file.-timebase 1/60000: Matches the tbn of the
first file.-c:a aac -ar 44100 -ac 2: Ensures audio
compatibility..mov File (Optional)If the original .mov file isn't already optimized for
concatenation, you can re-encode it with these settings to ensure full
compatibility:
ffmpeg -i input.mov -c:v h264 -profile:v high -pix_fmt yuv420p -s 1920x1080 -b:v 10549k -r 60 \
-timebase 1/60000 -c:a aac -ar 44100 -ac 2 reencoded_input.movCreate a file_list.txt for the re-encoded files:
file 'reencoded_input.mov'
file 'reencoded_output.mov'
Then concatenate them:
ffmpeg -f concat -safe 0 -i file_list.txt -c copy concatenated.movBy standardizing:
tbn): Aligning the timebase
prevents timestamp inconsistencies.Let me know if you encounter further issues!