LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2020, 12:57 PM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Question Bash script help needed to process a dvd


Okay, I can use lsdvd to see what titles and chapters there are on a dvd, easy

I can manually run HandbrakeCLI to process a Title and Chapter

I would like to create a bash script to use the output of lsdvd to loop through that output to run HandbrakeCLI to extract the Title, and optionaly Titles and Chapters, but my beginner skills in bash aren't good enough.

One idea I have is to use lsdvd -C > dvd-structure.txt, then to process that file. But then I get stuck, I'm not sure how to process that file.

Any tips or help much appreciated.

This is what lsdvd -c gives me:

Code:
charlie@charlie-machine:~$ lsdvd -c
Disc Title: WRONG TURN
Title: 01, Length: 01:29:28.920 Chapters: 13, Cells: 13, Audio streams: 01, Subpictures: 00
	Chapter: 01, Length: 00:09:39.720, Start Cell: 01
	Chapter: 02, Length: 00:06:44.560, Start Cell: 02
	Chapter: 03, Length: 00:09:24.600, Start Cell: 03
	Chapter: 04, Length: 00:06:26.920, Start Cell: 04
	Chapter: 05, Length: 00:09:17.360, Start Cell: 05
	Chapter: 06, Length: 00:09:44.800, Start Cell: 06
	Chapter: 07, Length: 00:09:56.960, Start Cell: 07
	Chapter: 08, Length: 00:06:59.280, Start Cell: 08
	Chapter: 09, Length: 00:06:20.280, Start Cell: 09
	Chapter: 10, Length: 00:04:44.160, Start Cell: 10
	Chapter: 11, Length: 00:07:06.680, Start Cell: 11
	Chapter: 12, Length: 00:03:03.120, Start Cell: 12
	Chapter: 13, Length: 00:00:00.480, Start Cell: 13
Title: 02, Length: 00:00:01.000 Chapters: 01, Cells: 01, Audio streams: 00, Subpictures: 00
	Chapter: 01, Length: 00:00:01.000, Start Cell: 01
Title: 03, Length: 00:00:12.000 Chapters: 01, Cells: 01, Audio streams: 00, Subpictures: 00
	Chapter: 01, Length: 00:00:12.000, Start Cell: 01
Title: 04, Length: 00:01:46.440 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
	Chapter: 01, Length: 00:01:46.440, Start Cell: 01
Maybe I need to use awk? Somehow I need to find Title: and/or Chapter: and from that I can create a handbrakeCLI command......
 
Old 01-17-2020, 01:29 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
If you just want the title:

Code:
lsdvd -c | head -n 1 | cut -d ' ' -f 3-
 
Old 01-17-2020, 03:48 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
It would be helpful if you post some sample output that you expect.
 
Old 01-18-2020, 11:37 AM   #4
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Question

Here is an example of what I'm trying to do:
Code:
charlie@charlie-machine:~/STRAWDOGS$ lsdvd
Disc Title: DVDVOLUME
Title: 01, Length: 00:00:27.600 Chapters: 02, Cells: 02, Audio streams: 01, Subpictures: 00
Title: 02, Length: 01:52:45.400 Chapters: 17, Cells: 19, Audio streams: 04, Subpictures: 00
Title: 03, Length: 00:07:43.200 Chapters: 03, Cells: 03, Audio streams: 01, Subpictures: 00
Title: 04, Length: 00:00:57.800 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Title: 05, Length: 00:00:31.200 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Title: 06, Length: 00:00:10.800 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
....
..
....
Title: 36, Length: 00:00:00.800 Chapters: 02, Cells: 02, Audio streams: 00, Subpictures: 01
Title: 37, Length: 00:00:57.960 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Title: 38, Length: 00:00:27.120 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Longest track: 02
charlie@charlie-machine:~/STRAWDOGS$
The Handbrake command is this to extract title 1 chapter 1
Code:
HandBrakeCLI -i /dev/sr0 -t 1 -c 1  -e x264 -b 1000 -r 29.97 -w 480 -o STRAWDOGS-T1-C1.mp4
and for title 1 chapter 2 its this
Code:
HandBrakeCLI -i /dev/sr0 -t 1 -c 2  -e x264 -b 1000 -r 29.97 -w 480 -o STRAWDOGS-T1-C2.mp4
and so on throughout the dvd, so I'm tryingto generate those commands from the result of lsdvd

I also want the option to ignore the chapters and just process the dvd title by title like this:
Code:
HandBrakeCLI -i /dev/sr0 -t 1 -e x264 -b 1000 -r 29.97 -w 480 -o STRAWDOGS-T1.mp4
HandBrakeCLI -i /dev/sr0 -t 2 -e x264 -b 1000 -r 29.97 -w 480 -o STRAWDOGS-T2.mp4
...
....
HandBrakeCLI -i /dev/sr0 -t 38 -e x264 -b 1000 -r 29.97 -w 480 -o STRAWDOGS-T38.mp4
Cheers folks
 
Old 01-18-2020, 11:38 AM   #5
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Question

Quote:
Originally Posted by dugan View Post
If you just want the title:

Code:
lsdvd -c | head -n 1 | cut -d ' ' -f 3-
I think you misunderstood my question, I want the dvd titles not the title of the dvd which works fine.

Code:
itle: 37, Length: 00:00:57.960 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Title: 38, Length: 00:00:27.120 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Longest track: 02
charlie@charlie-machine:~/STRAWDOGS$ lsdvd -c | head -n 1 | cut -d ' ' -f 3-
DVDVOLUME
charlie@charlie-machine:~/STRAWDOGS$

Last edited by GPGAgent; 01-18-2020 at 11:39 AM.
 
Old 01-18-2020, 01:35 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
I am neither familiar with lsdvd nor HandBrakeCLI but from the lists you posted this may do the job:
Code:
while read t c;do
        for (( i=1; i<=c; i++ ));do
                echo "Your handbrake with title: '$t' and chapter: '$i'"
        done
done < <(lsdvd | awk '{if ($1 ~ "Title"){printf "%d %d\n", $2, $6;}}' )
As you can see it iterates through all the titles with all available chapters. Insert the variables $t and $i into the HandBrakeCLI command as needed. Hope this helps.
 
Old 01-19-2020, 09:02 AM   #7
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by crts View Post
I am neither familiar with lsdvd nor HandBrakeCLI but from the lists you posted this may do the job:
Code:
while read t c;do
        for (( i=1; i<=c; i++ ));do
                echo "Your handbrake with title: '$t' and chapter: '$i'"
        done
done < <(lsdvd | awk '{if ($1 ~ "Title"){printf "%d %d\n", $2, $6;}}' )
As you can see it iterates through all the titles with all available chapters. Insert the variables $t and $i into the HandBrakeCLI command as needed. Hope this helps.
I like this, and I think the read line is processing the output of the lsdvd command at the end, so no need for file to hold that output and read later.

Looks neat, I'll give it a go.

Cheers

Last edited by GPGAgent; 01-19-2020 at 09:08 AM.
 
Old 01-19-2020, 09:13 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
https://stackoverflow.com/questions/...o-a-bash-array
something like this may work
Code:
while IFS=$'\n' read -d '' -r -a lines
do
   TITLE=${lines[1]}
   LENGTH=${lines[3]}
...
   # do whatever you want
done < dvd.txt
and you still need to handle , (commas).
 
Old 01-19-2020, 10:28 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by pan64 View Post
https://stackoverflow.com/questions/...o-a-bash-array
something like this may work
Code:
while IFS=$'\n' read -d '' -r -a lines
do
   TITLE=${lines[1]}
   LENGTH=${lines[3]}
   echo "${lines[@]}" # does not echo
   # do whatever you want
done < dvd.txt
and you still need to handle , (commas).
This reads the entire file and puts an entire line per array element. I cannot echo the array elements inside the loop, in fact, the loop is not entered at all. You can just use a single read to put the entire file into the array:
Code:
IFS=$'\n' read -d '' -r -a lines < dvd.txt
echo "${lines[@]}"
I am not sure this is what OP wants.
 
Old 01-20-2020, 12:42 PM   #10
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Some nice answers, some good bash scripts to extend my bash knowledge, but I found a good solution, which I guess could be automated a bit but it works for me

Step 1: use lsdvd to see how many titles on the dvd

Step 2: use vobcopy to extract the titles, this will extract 5 titles
Code:
for i in {1..5};do vobcopy -n $i -t Name-T -v -o .; done
Then I use ffmpeg to convert to mp4 or avi
 
Old 01-20-2020, 02:00 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
I dumped the lsdvd output to a file for testing. Here's a loop that will get you the titles and chapters.

Code:
$ cat strawdogs.txt 
Disc Title: DVDVOLUME
Title: 01, Length: 00:00:27.600 Chapters: 02, Cells: 02, Audio streams: 01, Subpictures: 00
Title: 02, Length: 01:52:45.400 Chapters: 17, Cells: 19, Audio streams: 04, Subpictures: 00
Title: 03, Length: 00:07:43.200 Chapters: 03, Cells: 03, Audio streams: 01, Subpictures: 00
Title: 04, Length: 00:00:57.800 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Title: 05, Length: 00:00:31.200 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Title: 06, Length: 00:00:10.800 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00
Longest track: 02
$ cat strawdogs.txt | awk -F "[, ]" '/^Title/ {printf("%s\t%s\n", $2, $7)}' | while read -r tch; do echo Title $(echo "$tch" | cut -f 1) Chapter $(echo "$tch" | cut -f 2); done
Title 01 Chapter 02
Title 02 Chapter 17
Title 03 Chapter 03
Title 04 Chapter 01
Title 05 Chapter 01
Title 06 Chapter 01
So this should work for you:
Code:
#!/usr/bin/env bash
lsdvd | awk -F "[, ]" '/^Title/ {printf("%s\t%s\n", $2, $7)}' | while read -r tch;
do
  title=$(echo "$tch" | cut -f 1)
  chapter=$(echo "$tch" | cut -f 2)
  echo "$title" "$chapter"
done
Change the 'echo $title" "chapter"' line to the actual Handbrake command.

Last edited by dugan; 01-20-2020 at 02:25 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
API for current running process details like process name, process id, amount of memory used anki123 Linux - General 1 01-20-2019 02:06 AM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 09:42 AM
Finding the Process ID of a Process While Initiating the Process senthilmuthiah Linux - Newbie 7 04-02-2009 10:37 AM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
DVD-R, DVD+RW, DVD-RW and DVD+R zetsui Linux - Hardware 5 09-12-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration