tanersener / mobile-ffmpeg

FFmpeg for Android, iOS and tvOS. Not maintained anymore. Superseded by FFmpegKit.
https://tanersener.github.io/mobile-ffmpeg
GNU General Public License v3.0
3.87k stars 791 forks source link

Reverse Command seems to not work #565

Closed ferPrieto closed 3 years ago

ferPrieto commented 4 years ago

Description I've split the video in several parts: _-i, srcPath, -c:v, libx264, -crf, 22, -map, 0, -segment_time, 6, -g, 9, -sc_threshold, 0, -force_key_frames, expr:gte(t,nforced*6), -f, segment, destPath Then I've tried to reverse those parts: -i, srcPath, -vf, reverse, -af, areverse, destPath

Expected behavior The video parts are reversed

Current behavior The process stops suddenly without showing any log. The last log it shows is: _cur_dts is invalid st:0 (0) [init:1 idone:0 finish:0] (this is harmless if it occurs once at the start per stream)

Environment

Other It seems to work when there's one piece of video only, which means the video is about 2 seconds. For longer video it crashes

tanersener commented 4 years ago

What do you mean by one piece of video? As far as I can see you only have one input file in your reverse command. Do you have multiple input files when there are multiple pieces of video?

ferPrieto commented 4 years ago

I've added a generic path as srcPath and destPath, but the actual path is something like: _-i, "src/video.mp4", -c:v, libx264, -crf, 22, -map, 0, -segment_time, 6, -g, 9, -sc_threshold, 0, -force_key_frames, expr:gte(t,nforced*6), -f, segment, "src/.SplitVideo/video0.mp4"

tanersener commented 4 years ago

I get that. You've said that it works when there's one piece of video. I'm trying to understand what that is?

ferPrieto commented 4 years ago

Yes, when the original video is about 2 seconds long, it does not need to get split in several segments, so there's just a unique piece to reverse. In that case, the reverse command goes through with no problems.

ferPrieto commented 4 years ago

Hi @tanersener , can we follow up this problem? Because I guess it might happen to other people in the future. This is the class I use for the FFMpeg processing (Reverse Video feature). I follow the next flow:

  1. Importing a video
  2. Splitting the video (if needed), in small chunks
  3. Reversing chunks
  4. Concatenate reversed chunks
  5. Export video

And the step that fails with no logs is the reversing chunks, with the log I posted in the first message of this issue. This is the concrete class I use: https://github.com/ferPrieto/Twinpeaks/blob/master/data/src/main/java/fernando/prieto/data/ffmpeg/FfmpegCommandExecutor.kt

Thanks, Fernando

tanersener commented 4 years ago

If videos shorter than 2 seconds are reversed successfully, for me this shows that your reverse command is correct. Therefore, I would concentrate on verifying that your splitting command is working as expected if I were you.

ferPrieto commented 4 years ago

Hey @tanersener , I've tried to change the way I split the videos. As you said, I've avoided using x264 and it seems that the problem is in the reverse command, which potentially consumes too much CPU. For that reason Android OS closes the app all of a sudden. My new split command is: _-i sourceFilePath -c copy -map 0 -segmenttime 6 -f segment destinationPath The split 6 second videos seem to be ok, I can reproduce them all.

I've also tried to use a different reverse command, with no better solution: _-i fragmentSourcePath -filtercomplex "[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]" -map [v] fragmentDestinationPath

There's no ffmpeg traces, simply a crash and the segments generated are 48 Bytes, which is wrong (they don't even play).

I found this trace at the bottom of my logs, referring to my app: Channel is unrecoverably broken and will be disposed!

tanersener commented 4 years ago

I don't have the answers for some of my questions. So, where is that 2 second limit coming from? Additionally, are those 6 seconds segments playable, they should be all playable.

ferPrieto commented 4 years ago

There was no second limit, I got the reverse command working only for short videos, such as 2 seconds. Any video longer than that crashed in the middle of the reverse process.

In regards to the 6s segments, they're playable as I indicated in the previous message. Hope that helps to spot the problem, although I reckon it's the CPU consumption.

tanersener commented 4 years ago

Do you have the chance to test your reverse commands using desktop ffmpeg? If not I'll ask you to provide me a sample application to reproduce this issue. I have to test it and see what's happening there.

ferPrieto commented 4 years ago

I don't have ffmpeg running on Desktop. I can try to find the set up for that soon though.

The sample Android app I'm working on is this one: https://github.com/ferPrieto/Twinpeaks

Thanks for this follow up

tanersener commented 4 years ago

Recorded a 8.7 second video in your app. This is the output https://ghostbin.co/paste/sork.

The following line is printed 37 times.

2020-10-14 21:51:57.641 25783-26679/prieto.fernando.twinpeaks D/FfmpegCommandExecutor: Started command : ffmpeg [-i, /storage/emulated/0/Android/data/prieto.fernando.twinpeaks/cache/VID_2020_10_14_21_51_19_529.mp4, -c:v, libx264, -crf, 22, -map, 0, -segment_time, 6, -g, 9, -sc_threshold, 0, -force_key_frames, expr:gte(t,n_forced*6), -f, segment, /storage/emulated/0/Android/media/prieto.fernando.twinpeaks/.VideoSplit/split_video%03d.mp4]

In the end, there are 2 split videos created. The first one is playable, the other one is not. The second one has wrong timestamp information. If you try to play it in a more advanced player it starts to play from 6th second until the 9th second.

221929 bytes - 6.14 seconds - split_video000.mp4
98006 bytes - 2.73 seconds - split_video001.mp4

If you look at the logs you can see the difference.

This is how video000.mp4 starts:

stream:0 start_pts_time:0 pts:1459 pts_time:0.128072 dts:691 dts_time:0.0606566

This is how video001.mp4 starts:

stream:0 start_pts_time:6.12807 pts:69811 pts_time:6.12807 dts:69043 dts_time:6.06066

After that, the following log line is printed 29 times. But it is only printed for video1 not for video0.

2020-10-14 21:52:16.668 25783-26679/prieto.fernando.twinpeaks D/FfmpegCommandExecutor: Started command : ffmpeg [-i, /storage/emulated/0/Android/media/prieto.fernando.twinpeaks/.VideoSplit/split_video001.mp4, -vf, reverse, -af, areverse, /storage/emulated/0/Android/media/prieto.fernando.twinpeaks/VideoPartsReverse/reverse_video1.mp4]

There are two reversed video outputs in the end. One of them is invalid, the other one is valid and playable.

48 bytes - 0 seconds - reverse_video0.mp4
97072 bytes - 2.73 seconds - reverse_video1.mp4

If you look at the reverse command outputs you can see the following errors for video000.mp4 and reverse_video0.mp4. I guess they show that there is something not supported in those videos. I'll suggest doing similar tests with longer videos or using shorter segment size to find the issue. Also try to set the correct timestamp for your videos.

Unknown dref type 0x206c7275 size 12
[h264 @ 0xd9853000] no picture 
ferPrieto commented 4 years ago

Thanks for sharing all of this @tanersener.

It's been really helpful to identify the time stamps issue of the split videos result. However, I've been trying to find the exact command to avoid that and I still keep looking for any.

It surprises me because, I've been trying with different segment times (longer and shorter), with numerous sizes source videos and I've never got the concatinating step at all. I always get the 48bytes reversed videos.

I'll keep you posted, let me know if there's anything I could try for the wrong timestamps. Thanks for the help

ferPrieto commented 3 years ago

Hey @tanersener , after trying so many different commands I've not found any solution about the wrong time stamps. Do you know a generic way to do this and not screwing up the time stamps?

Thanks in advance

tanersener commented 3 years ago

I'm using setpts=PTS-STARTPTS to fix the timestamps. See the documentation about it.

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.