2

I have these example files in an Ubuntu 18.04 system:

VID_20190407_160033.3gp  
VID_20190407_161444.3gp  
VID_20190407_161609.3gp  

VID_20190415_183315.3gp  
VID_20190415_183411.3gp  
VID_20190415_192712.3gp  

VID_20190420_124435.3gp  
VID_20190420_125755.3gp  
VID_20190420_130214.3gp  
VID_20190420_141700.3gp  

And I want to concatenate 3GP files by a script (perhaps with ffmpeg ?) into one file, selecting files by the YYYYMMDD date in the file name, with a result of:

VID_20190407.3gp
VID_20190415.3gp
VID_20190420.3gp

What is the best way to script that?

System

Linux local 5.0.0-29-lowlatency #31-Ubuntu SMP PREEMPT Thu Sep 12 14:13:01 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Thank you.

K7AAY
  • 16,864
  • 9
  • 45
  • 77
genderbee
  • 830
  • 1
  • 14
  • 31

1 Answers1

3

3GP files can be concatenated with the use of ffmpeg by a command similar to:

ffmpeg -f concat -i <(find . -name 'YYYYMMDD_XXXXXX.3gp' -printf "file '$PWD/%p'\n") -c copy YYYYMMDD.3gp

once you create a for-next loop to filter on the YYYYMMDD using the first instance of the first value for that as a variable, collect the _XXXXXX file name components, and step through them sequentially; then, move on to the next YYYYMMDD value and step through all its files.

--

An alternate and more elegant method using a virtual concat demuxer which has brought to my attention by llogan would be to

A) count the number of files with the first YYYYMMDD prefix, then
B) create a control file mylist.txt with a line for each of the files matching that date prefix, which would look like:

$ cat mylist.txt
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
file '/path/to/file4'

or

$ cat mylist.txt
file '/path/to/YYYYMMDD_XXXXX1.3gp'
file '/path/to/YYYYMMDD_XXXXX2.3gp'
file '/path/to/YYYYMMDD_XXXXX3.3gp'
file '/path/to/YYYYMMDD_XXXXX4.3gp'

then C) execute

ffmpeg -f concat -i mylist.txt -c copy YYYYMMDD.mp4

Now, you've concatenated the first file set. Step on to the next YYYYMMDD group and repeat.

K7AAY
  • 16,864
  • 9
  • 45
  • 77
  • 1
    MP4 can be concatenated with the concat demuxer (as shown in the answer), but not directly with the concat protocol (in the link). Not need to remux to ts and use the concat protocol. Just use concat demuxer. – llogan Sep 23 '19 at 22:48
  • See "Using intermediate files" in http://trac.ffmpeg.org/wiki/Concatenate#protocol for info on concatenation of MP4 files via transforming them to MPEG-2 files first. Warning: MPEG-2 files are at least 2X as large as MP4 files, so have lots of space available. As to the for-next loop, I am not adept enough to know what to do, all I know is what to use to do it. – K7AAY Sep 23 '19 at 22:51
  • 1
    @K7AAY https://superuser.com/questions/1446436/why-is-the-results-of-this-ffmpeg-command-a-broken-mp4-file/1448026#1448026 https://superuser.com/questions/1450652/ffmpeg-only-merges-1-video-when-using-filter-complex/1450665#1450665 https://stackoverflow.com/questions/43536929/ffmpeg-concat-video-finished-but-the-video-missing/43543298#43543298 https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg/11175851#11175851 – llogan Sep 23 '19 at 23:18
  • @llogan Excellent! Those are worthy of an answer here, should you wish to write it up. Hope you do. – K7AAY Sep 23 '19 at 23:30
  • 1
    @K7AAY Your answer seems good to me. – llogan Sep 24 '19 at 00:54
  • 1
    @K7AAY How can I filter founded files by `$YYYYMMDD`? I tried `sort` and `uniq -w`, but seems it is not best solution. Better way would be to detect `YYYYMMDD` format and write all files by this date. Thanks. – genderbee Sep 24 '19 at 08:56
  • @genderbee As to the for-next loop, I am not adept enough to know what to do, all I know is what to use to do it. – K7AAY Sep 24 '19 at 16:54