OT: Can I losslessly invert an MP4 video?

PeterDuke wrote on 12/29/2015, 7:57 PM
If I hold my mobile phone in the wrong landscape orientation when recording a video, the video appears upside down when viewed on the computer. Is there a lossless way of inverting such a video?

I saw a Youtube video where the presenter used Free Video Flip and Rotate to invert the image for playback on a computer, but it was still upside down when uploaded to Youtube. Several viewers also commented that the utility is loaded with malware.

Comments

musicvid10 wrote on 12/29/2015, 8:55 PM
There is -- but it's not not quite lossless in Vegas because pixel interpolation is not quite perfect.
Last time I had to do this I believe it was with GoPro Studio, and with quite good results.

All the command-line utilities (Avisynth/MeGUI, ffmpeg, etc.) should do this as well.
PeterDuke wrote on 12/30/2015, 6:49 AM
Thanks. I'll explore them.

I wouldn't have thought pixel interpolation was necessary. Merely rotation of each frame by 180 degrees (or perhaps each GOP) (or perhaps I am just ignorant).
musicvid10 wrote on 12/30/2015, 8:27 AM
I actually ran one of my silly tests a few years ago and found differences from the original in the way Vegas does it.
Interpolation may be an inaccurate choice of words; remapping to new coordinates might be closer to what goes on.
PeterDuke wrote on 12/30/2015, 5:00 PM
Well, the dumb straight forward way would be to fully decode each frame, rotate 180 degrees and then re-encode. Perhaps that is what Vegas does.

t seems to me that there should be a way to rotate losslessly, just as can be done for JPEG images. MPEG is further complicated by the interframe predictive coding, but that should only be a complication, not something insurmountable. All coefficients applying to pixel (x,y) are "simply" transferred to pixel (N-x-1, M-y-1), where the frame size is NxM pixels.
John_Cline wrote on 12/30/2015, 5:26 PM
Given the math involved and the complex intraframe encoding of MP4 files, I know of no way to losslessly invert the video without a decoding/encoding cycle.
musicvid10 wrote on 12/30/2015, 5:48 PM
Correct. Only one in every 300 frames or so constitutes an actual image, called an i-frame.
Incomplete macroblocks would result in errored or "filled in" bits.


DeadRadioStar wrote on 12/30/2015, 8:11 PM
Just turn your TV upside down.
I'll get me coat ....
TheHappyFriar wrote on 12/30/2015, 8:36 PM
Maybe rendering to a PNG image sequence & then re-rendering a new video would be the highest quality way. Except for the "rotate TV" method. Works every time. Windows even let's you flip the display w/o physically doing it so that might work too. :)
John_Cline wrote on 12/30/2015, 11:22 PM
Rendering to a PNG image sequence would be absolutely no different than just having Vegas rotate the video. Of course, the visual quality of the subsequent encoding to MP4 will be significantly affected by the choice of MP4 encoding parameters and bit rate. Rotating the video is the easy part and Vegas is perfectly capable of this.
imaginACTION_films wrote on 12/31/2015, 12:44 AM
Use your mobile phone to capture a video the right way up, then put a Dymo label on the camera saying THIS WAY UP. Then you'll never have to flip the video...
Happy New Year, everyone!
David
Chienworks wrote on 12/31/2015, 7:24 AM
"All coefficients applying to pixel (x,y) are "simply" transferred to pixel (N-x-1, M-y-1), where the frame size is NxM pixels."

The problem is that in highly compressed formats like MP4 with GOP encoding, there are no pixels in the data stream. All you have is mathematical descriptions of amorphous waveforms that represent an approximation of the original image, and that only on I frames. In between the I frames are merely differences between the previous I frame and the next.

The only way to pull pixel data out of that is to decompress and decode it completely, of course with the concomitant loss of quality that the original encoding introduced. If you then want an MP4 file out of it after the transformation then you have to re-encode and compress it again, introducing more quality loss.
Gyan wrote on 1/1/2016, 7:13 AM
You may not need to actually transpose the stored matrices (if no flipping is needed) . If the video player understands embedded rotation tag, then the following ffmpeg command will quickly (and losslessly) indicate to the video player on how to orient the decoded video stream


ffmpeg -i input.mp4 -c copy -metadata:s:v rotate=180 output.mp4
PeterDuke wrote on 1/1/2016, 6:57 PM
The trouble with the rotation tag is that some apps ignore it (MPC-HC and VideoReDo for instance).

The tag is apparently already set. I was looking for a universal solution and I suffer from a tidy mind :(
PeterDuke wrote on 1/1/2016, 7:24 PM
Lossless rotation of MP4 by 180 degrees seems like it should be possible in principle, but after an extensive search I have found many people looking for it but no solutions.

One person said that the matrices are scanned in a certain order during decoding, and rotation of the matrices (as can be done with JPEG for instance) would lead to non-standard scanning. I know not whether this is true or not, nor whether it is important (Jelly wobble?)