Videos - conseqences of doing this.

Started by Alan Clifford, July 30, 2023, 09:35:52 AM

Previous topic - Next topic

Alan Clifford

Having bought a trail camera to catch the foxes in the garden, I'm left with 60 MB or bigger files.  I'm trimming out the dead time with ffmpeg.

However, I sometimes want to keep the full 20 seconds night movie.  If I run it though ffmpeg, this example reduces to 27 MB.

What am I losing?

Process is this (in a bash script):

DUMMY=$(ffmpeg -ss ${START} -to ${END} -i ${IN} -map_metadata 0  ${OUT})

folowed by

DUMMY=$(addxmpdate.pl "${OUT}" -updatexmp -touch -allowqtcd)

(the later to add xmp:datetime original from the quicktime:createdate based on a file containing timezone information)

I see a difference in average bit rate, an audio chanel has been added and the compressor is different.

Attached are the exiftool outputs from both files together with a diff of these.



StarGeek

I would run the original with the -ee3 (-extractEmbedded3) option just to make sure there isn't some data in the streams, though I don't think there will be for a static location camera.

Other than that, it doesn't look like it puts any extra info in the file such as an EXIF block, so there's nothing to lose there.  The audio started as sowt, which is uncompressed PCM audio (had to look that up), so the second probably doesn't lose very much quality with the compression there.

You are going to lose a bit of quality since this is recompressing the video stream, but almost certainly not enough to really notice, assuming this is a 1st generation copy.

The only other things I'd say to look into would be to either try for a lossless cut and copy, but that would require knowing where the key frames are and I have no idea how to do that for ffmpeg.  I use AVIDemux for that.  Or if your computer is powerful enough or you don't mind waiting, you might try h265 as the compression, as that can get you small file sizes for the same bit rate.

Video and ffmpeg are not my strongest area, so that's about all I can think of.

"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Alan Clifford

Hello @StarGeek

I'm looking at the ffmpeg documentation;  it is somewhat comprehensive!

I've tried using h.265.  Massive reduction in file size but also a massive reduction in average bit rate.  I'm assuming this is a bad thing.  But I'll try using it when I scale the size down for posting "on the internet".  But there are many options to use with the encoder so I need to read more.

wywh

I'd use only:

ffmpeg -ss 00:00:01 -to 00:00:02 -i movie.mp4 -c copy trim.mp4

...and use exiftool tagsFromFile to copy desired tags from the source if there are any worth copying.

- Matti

StarGeek

Quote from: Alan Clifford on July 30, 2023, 12:53:35 PMMassive reduction in file size but also a massive reduction in average bit rate.  I'm assuming this is a bad thing

My understanding is that h265 is a better compression so that you can get the same quality with a smaller bitrate vs h264.  But as I said, video isn't my strongest area.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Alan Clifford

Quote from: wywh on July 30, 2023, 02:47:06 PMI'd use only:

ffmpeg -ss 00:00:01 -to 00:00:02 -i movie.mp4 -c copy trim.mp4

...and use exiftool tagsFromFile to copy desired tags from the source if there are any worth copying.

- Matti


I get an error  with -c copy

[mp4 @ 0x7ffa38511840] Could not find tag for codec pcm_s16le in stream #1, codec not currently supported in container
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Error initializing output stream 0:1 --


StarGeek

I believe the problem there is that MP4 doesn't support PCM audio.  At least that's what I'm guessing.  I have yet to find a simple list of which audio/video codecs are supported by MP4s.  MKVs will accept about any format.

So the audio will need to be converted to a different format.  I think what you would want to use would be something like
-acodec AAC -vcodec copy
which would copy the video stream and convert the audio stream to AAC.  Or convert it any other audio format that MP4 can accept.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

wywh

Quote from: Alan Clifford on July 30, 2023, 03:01:00 PM-c copy

Yes, .mp4 does not accept PCM audio (or DV, ProRes video etc). But .mov does so this seems to work:

ffmpeg -ss 00:00:01 -to 00:00:02 -i a.dv -c copy trim.mov

Or re-encode video and audio to something that .mp4 supports (here DV and PCM re-encoded to H.265 and AAC):

ffmpeg -ss 00:00:01 -to 00:00:02 -i a.dv -c:v libx265 -tag:v hvc1 -c:a aac -b:a 128k trim.mp4

...but I wonder how PCM can be in the original .mp4?

- Matti



StarGeek

Quote from: wywh on July 31, 2023, 03:12:20 AM...but I wonder how PCM can be in the original .mp4?

Just because it's not allowed doesn't mean that companies won't shove it in there.

Plus, I think there's an ffmpeg option to allow such files.  I remember seeing something about how to put OGG audio into MP4s, which wasn't allowed at the time (not sure about currently).
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Alan Clifford

I don't have any problem with audio using the command line in the first post.

wywh

#10
Quote from: StarGeek on July 31, 2023, 01:52:54 PMI think there's an ffmpeg option to allow such files

For fun I tried to create .mp4 with PCM audio (and ProRes or H.265 video) but ffmpeg fails even with '-strict -2' option (-2 or experimental -- allow non-standardized experimental things).

BTW it seems that, like macOS 13 QuickTime Player, also ffmpeg trims to the GOP's I frame. It is up to the viewer whether it displays the trimmed BP frames or not.

Avidemux is great for interactively displaying IBP frames and it displays also such trimmed BP frames.

On the other hand, QuickTime Player displays only the trimmed movie and omits those extra BP frames.

I tried to inspect the .mp4 but could not find what info tells the viewer what is the trimmed part and what is that "extra" content. Anyone?

Usually that is not a big deal, but some movies might have long GOPs (I frame every 2 seconds or so) and I sometimes edit with Avidemux to the GOP so no viewer unintentionally displays such extra frames. Some apps can re-encode the "extra" BP frames at trim points to contain an I frame but that is lossy. MPEG is a delivery format, not an editing format...

https://avidemux.sourceforge.net

p.s. Like ffmpeg, also QuickTime Player mangles metadata in the trimmed movie. QuickTime Player converts metadata in Keys to UserData that it can still display. ffmpeg '-movflags use_metadata_tags -map_metadata 0' preserve Keys and QuickTime:CreateDate but obviously in some nonstandard way that QuickTime Player or Photos.app no longer displays.

One fix for that is to use exiftool tagsFromFile to copy desired tags from the original:

ffmpeg -ss 00:00:01 -to 00:00:02 -i movie.mp4 -c copy -movflags use_metadata_tags -map_metadata 0 trim.mp4

exiftool -m -overwrite_original -tagsFromFile movie.mp4 -All:All trim.mp4

Also simply rebuilding metadata seems to fix that:

ffmpeg -ss 00:00:01 -to 00:00:02 -i movie.mp4 -c copy -movflags use_metadata_tags -map_metadata 0 trim.mp4

exiftool -m -overwrite_original -All= -tagsFromFile @ -All:All -Unsafe -ICC_Profile trim.mp4

- Matti