Skip to content

Re-encrypt segment error: get full samples: offset in mdata beyond size #378

Closed
@xzerth

Description

Hello! I have a usecase when I need to reencrypt segments: so I need to decrypt it and then encrypt again (with different key probably).
In general it combines the code from two examples: decrypt and encrypt.

Something like this:

func main() {
	key := []byte{
		0x69, 0x4F, 0x3E, 0xAA,
		0xBD, 0x56, 0xA2, 0x58,
		0x28, 0x2E, 0xE3, 0x0E,
		0x99, 0x6A, 0x32, 0x18,
	}
	keyIV := make([]byte, 16)

	initFile, err := os.ReadFile("init.m4i")
	encryptedVideoFile, err := os.ReadFile("video.m4v")
	initMp4, err := mp4.DecodeFile(bytes.NewReader(initFile))
	decryptInfo, err := mp4.DecryptInit(initMp4.Init)

	mp4File, err := mp4.DecodeFile(bytes.NewReader(encryptedVideoFile))
	for _, seg := range mp4File.Segments {
		err := mp4.DecryptSegment(seg, decryptInfo, key)
		if err != nil {
			fmt.Printf("Error decrypt segment: %v\n", err)
		}
	}

	// Uncomment this and error is gone
	// var buf bytes.Buffer
	// mp4File.Encode(&buf)
	// mp4File, err = mp4.DecodeFile(bytes.NewReader(buf.Bytes()))

	protectData, err := mp4.InitProtect(initFile.Init, key, keyIV, "cenc", key, decryptInfo.Psshs)
	for _, s := range mp4File.Segments {
		for _, f := range s.Fragments {
			err := mp4.EncryptFragment(f, key, keyIV, protectData)
			if err != nil {
				// HERE WE'VE GOT AN ERROR
				fmt.Printf("Error encrypting fragment: %v\n", err)
			}
		}
	}
}

And then it returns an error: get full samples: offset in mdata beyond size.
If I decrypt, then Encode() to buffer, then decode again and encrypt - it works. Can't attach init and video files here for some reason. Let me know if you need them, but I don't think it is the particular stream that causes this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions